Operators

Let us understand the below terms before we get into more details.

1. Operator

An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. In the below example, + is the operator.

let x = 2, y= 3;
let sum = x + y;
console.log(sum); // prints 5

2. Operand

An operand is what operators are applied on. In the above example x and y are the operands.

3. Unary

If the operator is applied on a single operand then it is called unary. In the below example, + doesn't change the value of x. + operator will not do anything to numbers but converts strings or boolean values to numbers. This is equal to Number() method.

let x = -4;
console.log(+x); // There will be no effect on numbers;

let y= true;
console.log(y); // prints true
console.log(+y); // prints 1
let x = 4;
x = -x; // Unary nagation 
console.log(x); // prints -4

4. Binary

If the operator is applied on a two operands then it is called binary.

let x = 2, y= 3;
let multiply = x * y; // binary * operator
console.log(multiply); // prints 6

5. Ternary

If the operator is applied on a three operands then it is called ternary. This is also known as conditional operator as a condition is followed by ? and true-expression which is followed by a : and false expression. This is oftenly used as a shortcut to replace if-else statement

let x = 2, y= 3;
let z = x > y ? x : y; // z will be assigned with x if x is greater than y and viceversa
console.log(z); // prints 3

If we need to write the same logic using If-else, the code looks bigger with more statements. Hence ternary operator is used as a shortcut for these kind of scenarios.

let x = 2, y= 3;
let z;

if ( x > y) {
    z = x;
} else {
    z = y;
}; 
console.log(z); // prints 3

Operators in Javascript

1. Arithmetic Operators

JavaScript arithmetic operators are used to perform arithmetic operations on operands.

OperatorDescriptionExample
+Used to perform Addition8+2 = 10
-Used to perform Subtraction12-2 = 10
*Used to perform Multiplication5*2 = 10
/Used to perform Division100/10 = 10
%Used to perform Remainder40%10 = 0
++Used to perform Incrementlet a=10; a++; // a becomes 11
--Used to perform Decrementlet a=10; a--; // a becomes 9

2. Comparison Operators

JavaScript comparison operators are used to compare two operands.

OperatorDescriptionUsage
==Is equal tox == y
===Same as is equal tox === y
!=Not equal to!=x
!==Not Identical tox!==x
>Greater thanx > y
>=Greater than or equal tox >= y
<Less thanx < y
<=Less than or equal tox <= y

3. Bitwise Operators

JavaScript bitwise operators are used to perform bitwise operations on operands.

OperatorDescriptionUsage
&Bitwise AND(x > y) & (y > z)
|Bitwise OR(x > y) | (y > z)
^Bitwise XOR(x > y) ^ (y > z)
~Bitwise NOT(~x)
<<Bitwise Left Shiftx << y
>>Bitwise Right Shiftx >> y
>>>Bitwise Right Shift with Zerox >>> y

4. Logical operators

Below are the logical operators present in the Javascript.

OperatorDescriptionUsage
&&Logical AND(x > y) && (y > z)
||Logical OR(x > y) || (y > z)
!Logical NOT(!x)

5. Assignment Operators

Below are the assignment operators present in the Javascript.

OperatorDescriptionUsage
=Assignlet x = 10;
+=Add and assignlet x=10; x+=30; // x becomes 40
-=Subtract and assignlet x=40; x-=10; // x becomes 30
*=Multiply and assignlet x=10; x*=40; // x becomes 400
/=Divide and assignlet x=100; x /= 10;// x becomes 10
%=Modulus and assignlet x=100; x%=10; // x becomes 0

6. Special Operators

Below are the special operators present in javascript.

OperatorDescription
?:Conditional or ternary Operator, shortcut to if-else
,Allows multiple expressions to be evaluated as single statement.
newcreates an instance
typeofchecks which type of object.
voidused to discards the return value of an expression.
yieldused to check what is returned in a generator by the generator's iterator.
deleteto delete a property from the object.
inused to check if object has the given property
instanceofused to check if the object is an instance of given type