Javascript Operators

Siddhant Kapadne
5 min readApr 30, 2021

In this, we will get to know all the operators in detail with examples.

What are Operators?

  • Operators in Javascript are symbols that tell our interpreter to perform logical operations, mathematical operations, or conditional operations.
  • There are two categories of operators: binary operator and unary operator, But there is also one special category that is a ternary operator which will be discussed in one of the types that are the conditional operator.

1.Binary Operator: This Operator has two operands, where the operator is in between these two operands.

2. Unary Operator: This Operator has a single operand and the operator is either before the operand or after the operand.

Following are the types of Operators in Javascript

  1. Assignment Operator.
  2. Arithmetic Operator.
  3. Comparison Operator.
  4. Conditional Operator. (Special Operator ternary)
  5. Logical Operator.

Now let’s Start with digging deep into all these operators one at a time.

1. Assignment Operator

  • The job of the Assignment operator is to assign the value to the left side operand based on the value of the right side operand.
  • In simple words, take an example of = operator, what = operator does is that it takes the value from the right side operator and assigns it to the left side operator.
  • Example a = b which means a holds the value of b. (value of b is assigned to a)

Here are some popular Assignment operators types:

a = b  

As we discussed previously that a holds the value of b

a += b 

Over Here a is added to its own value and b,But value is stored in a, that is a = a + b .

a -= b 

a is subtracted to its own value and b,But value is stored in a, that is a = a — b

a *= b

a is Multiplied by its own value and b,But value is stored in a, that is a = a * b

a /= b

a is Divided by its own value and b, But value is stored in a, that is a = a / b

a %= b

For this it takes modulus of both a and b, But value is stored in a, that is a = a % b

a &&= b

In this logical AND is used for assignment, that is

a && (a = b)

a ||=b

In this logical OR is used for assignment, that is a ||(a = b)

Now let’s check how practically it is possible

Try this code in https://jsfiddle.net/ and also try %= on your own.

2. Arithmetic Operator

  • The job of the Arithmetic Operator is to perform an Arithmetic operation on two numeric operands either can be literals or can be variables.
  • After performing the Arithmetic operation it provides a single numerical value.
  • Basic Arithmetic operators are + , , * , / .
a = b + ca = b — ca = b * ca = b / c

Note : Divide by zero leads to Infinity in javascript

Here are some popular Arithmetic Operator types:

a = b % c

Remainder of b % c is generated and stored in a

a++

Current value of a is incremented by 1

a--

Current value of a is decremented by 1

Now let’s check how practically it is possible

Practice this code by yourself to be confident about it and also try divide operator on your own.

3. Comparison Operator

  • As the name suggests this operator’s Job is to compare the values of operands and return a logical value only if the comparison is true.
  • These operands can be anything it can be string, number, object values.
  • Conditional operators are mostly used with if-else, else if for comparing and returning required results.

Here are some popular Comparison Operators.

  1. Equality Operator (==): Compares two operands and returns true if both are equal.
  2. Strict Equality Operator(===): Compare two operands and return true if both are equal as well as of the same type.
  3. Not Equal (!=): Compare two operands and return true if both are not equal.
  4. Strict Not Equal (!==): Compare two operands and return true if both are not equal and have different types.
  5. Greater than ( > ): Returns true if the left side operand is greater than the right side operand.
  6. Greater than or equal ( >= ): Return true if the left side operand is greater or equal to the right side operand.
  7. Less than ( < ): Return true if the left side operand is less than right side operand.
  8. Less than or equal ( <= ): Return true if the left side operand is less or equal to the right side operand.

Now let’s check how practically it is possible

Practice this code by yourself to be confident about it and also try remaining types of comparison operators.

4. Conditional Operator

  • As it was mentioned above that there is a special ternary operator in javascript.
  • This operator in javascript takes three operands.
  • So the operator can have one or two values as per the condition(check).
  • It is a very important operator used in javascript to carry out difficult comparisons easily.
  • Syntax of ternary operator (Conditional Operator)

Note: It is readed as Check the condition if yes return valueA if no return valueB

Now let’s check how practically it is possible

Practice this code by yourself to be confident about it

5. Logical Operator

  • The job of a logical operator is to compare two sets of operations and then return the Boolean value true or false depending on an operator to operator.

There are three types of logical operators:

  1. Logical AND denoted as && which compares two conditions or operands and returns value only if both of them are true.
  2. Logical OR denoted as || which compares two conditions or operands and returns a value if any one of the two conditions is true.
  3. Logical NOT denoted as ! which compares two operands and returns a value if not equal true if equal then false.

Now let’s check how practically it is possible

Practice this code by yourself to be confident about it

Bonus

typeof:

  • This operator returns the type of the variable value that is a string, number, boolean, object, etc.
  • For example:

To know more about javascript Operator Visit:

Tip:

  • Use console.log() to know the output better which will be displayed in the console section of your browser.
  • Reading someone else’s code is good But it’s best to practice the same, So we gain confidence about the topic.
  • Follow for more Javascript basics with Siddhant.

Thank you for reading

--

--