JavaScript – Operators


What is an operator?

An operator is a character that represents an action.

What is an operator in JavaScript ?

Operators are termed as special symbols that perform specific operations on one, two, or three operands, and then returns the result.

What are the types of operators ?

There are 6 types of operators in JavaScript.

  1. Arithmetic Operators

2. Relational Operators

3. Bitwise Operators

4. Logical Operators

5. Assignment Operators

6. Special Operators

1. Arithmetic Operators

What is an arithmetic operator ?

Javascript supports various arithmetic operations such as Addition / subtraction / multiplication / division etc.

1. Addition


The addition of two numbers is done by Addition operator “+” .

Example :

var a = 10;
var b = 30;
var c = a+b;

Output :

40

2. Subtraction

Subtraction of two numbers is done by the subtraction operator “-” .

Example :

var a = 10;
var b = 30;
var c = b-a;

Output :

20

3. Multiplication

Multiplication of two numbers is done by the Multiplication operator “*” .

Example :

var a = 10;
var b = 30;
var c = a*b;

Output :

300

4. Division

Division of two numbers is done by Division operator “/” .

Example :

var a = 10;
var b = 30;
var c = b/a;

Output :

3

5. Modulus

The Modulus of two numbers is calculated by Modulus operator “%” .

Example :

var a = 10;
var b = 30;
var c = b%a;

Output :

0

6. Increment

Increment operator for a variable is done by “++”.

Example :

var a = 10;
a = a++; // code will be executed like this (a = a+1)

Output :

a = 11

7. Decrement

Decrement operator for a variable is done by “–“.

Example :

var a = 10;
var a = a--; // code will be executed like this (a = a-1)

Output :

9

2. Relational Operator

Javascript has eight relational operators that compare two variables and return a boolean value.

1. Is equal to (==)

This checks if the values of two operands are equal or not, if yes then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

a == b returns false.

2. Identical (===)

This checks if the values of two operands are equal and also with same type or not, if yes then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

a === b returns false.

3. Not equal to (!=)

This checks if the values of two operands are equal or not, if values are not equal then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

a != b returns true.

4. Not Identical (!==)

This checks if the values of two operands are not identical, if values are not identical then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :


a !== b returns true.

5. Greater than (>)

This checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

b > a returns true.

6. Greater than or equal to (>=)

This checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

b >= returns true.

7. Less than (<)

This checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

a < b returns true.

8. Less than or equal to (<=)

This checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Example :

var a = 10;
var b = 20;

Output :

a <= b returns true.

3. Bitwise Operators

What is Bitwise operators in Java ?

Bitwise operators operate on bitwise operations on operands.

1. AND (&)

Bitwise AND operator is executed by using “&” symbol.

Example :

var a = 10;
var b = 20;
var c = 30;

Output :

(( a == b ) & ( b == c)) returns false.

2. OR (|)


Bitwise OR operator is executed by using the “|” symbol.

Example :

var a = 10;
var b = 20;
var c = 30;

Output :

(( a == b ) | ( b == c)) returns false.

3. XOR (^)

Bitwise XOR operator is executed by using the “^” symbol.

Example :

var a = 10;
var b = 20;
var c = 30;

Output :

(( a == b ) ^ ( b == c)) returns false.

4. NOT (~)

Bitwise NOT operator is executed by using “~” symbol.

Example :

var a = 10;

Output :

(~a) = -10

5. Left Shift (<<)

Bitwise Left Shift operator is executed by using the “<<” symbol.

Example :

var a = 10;
var b = 2

Output :

(a<<b) = 40

6. Right Shift (>>)

Bitwise Right Shift operator is executed by using the “>>” symbol.

Example :

var a = 10;
var b = 2;

Output :

(a>>b) = 2

7. Right Shift with Zero (>>>)

Bitwise Right Shift with Zero operator is executed by using the “>>>” symbol.

Example :

var a = 10;
var b = 2;

Output :

(a>>>b) = 2

4. Logical Operators

1. Logical AND (&&)

Logical AND can be executed by using “&&” symbol.

Example :

var a = 10;
var b = 20;
var c = 30;

Output :

((a == b) && (b == c)) returns false

2. Logical OR (||)

Logical OR can be executed by using the “||” symbol.

Example :

var a = 10;
var b = 20;
var c = 30;

Output :

((a == b) || (b == c)) returns false

3. Logical NOT (!)

Logical NOT can be executed by using the “!” symbol.

Example :

var a = 10;
var b = 20;
var c = 30;

Output :

!(a == b)  returns true

!(b == c)  returns true

5. Assignment Operators

What are Assignment Operators ?

An assignment operator is used to assign value to a variable.

SnoSymbolDescriptionExample
1=Assigna=10;
2+=Add and Assignvar a=10; a+=20; Now a = 30
3-=Subtract and Assignvar a=20; a-=10; Now a = 10
4*=Multiply and Assignvar a=10; a*=20; Now a = 200
5/=Divide and Assignvar a=10; a/=2; Now a = 5
6%=Modulus and Assignvar a=10; a%=2; Now a = 0

1. Assign (=)

Assign operator can be executed by using the “=” symbol.

Example :

var a = 10;
var b = 20;
var c = a+b;

Output :

c is assigned to the sum of a and b.

Now c = 30;

2. Add and assign (+=)

Add and assign operator can be executed by using the “+=” symbol.

Example :

var a=20; 
a+=20; 

Ouput :

Now a = 40

3. Subtract and assign (-=)

Subtract and assign operator can be executed by using the “-=” symbol.

Example :

var a=20; 
a-=10; 

Ouput :

Now a = 10

4. Multiply and assign (*=)

Multiply and assign operator can be executed by using the “*=” symbol.

Example :

var a=5; 
a*=20; 

Output :

Now a = 100

5. Divide and assign (/=)

Divide and assign operator can be executed by using the “/=” symbol.

Example :

var a=10; 
a/=2; 

Output :

Now a = 5

6. Modulus and assign (%=)

Modulus and assign operator can be executed by using the “%=” symbol.

Example :

var a=10; 
a%=2; 

Output :

Now a = 0

Javascript online learning !!!

Click to Learn More about – Javascript online learning

6. Special Operators

1. Conditional Operator (?:)

This returns value based on the condition. It is similar to if-else.

2. Comma Operator (,)

This allows multiple expressions can be evaluated in a single statement.

3. Delete Operator

This deletes a property from the object.

4. In Operator

This checks if the object has the given property or not.

5. InstanceOf Operator


This checks if the object is an instance of given type or not.

6. New Operator

This creates an instance / example of the object.

7. TypeOf Operator

This checks the type of object.

8. Void Operator

This discards the expression’s return value (This will not return any value).

9. Yield Operator

This checks what is returned in a generator by the generator’s iterator.