JavaScript Operators

Symbols that are used on operands to perform operation are known as JavaScript operators.

							
var x = 3;         // assign the value 3 to x
var y = 2;         // assign the value 2 to y
var z = x + y;     // assign the value 5 to z (x + y)

What are Java script operators?

There are following types of operators in JavaScript.

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Special Operators

JavaScript Arithmetic Operators

Arithmetic operators are used when you want to perform some mathematical function on any number, variables or literals.

Operator Description Example
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
* Multiplication 10*20 = 200
/ Division 20/10 = 2
% Modulus (Remainder) 20%10 = 0
++ Increment var a=10; a++; Now a = 11
-- Decrement var a=10; a--; Now a = 9

JavaScript Comparison Operators

As the name itself suggests, this type of operator is used to compare two operand.So if you want to write a program to check equal, not equal,identical or not identical, greater or less than you will need comparison operator.. The comparison operators are as follows:

Operator Description Example
== Is equal to 10==20 = false
=== Identical (equal and of same type) 10==20 = false
!= Not equal to 10!=20 = true
!== Not Identical 20!==20 = false
> Greater than 20>10 = true
>= Greater than or equal to 20>=10 = true
< Less than 20<10 = false
<= Less than or equal to 20<=10 = false

JavaScript Bitwise Operators

Rather than treating values as decimals and octal decimal the operator treats operand as a binary sequence, but the output is values or numbers. They perform functions like and, or, shift right, shift left etc.. The bitwise operators are as follows:

Operator Description Example
& Bitwise AND (10==20 & 20==33) = false
| Bitwise OR (10==20 | 20==33) = false
^ Bitwise XOR (10==20 ^ 20==33) = false
~ Bitwise NOT (~10) = -10
<< Bitwise Left Shift (10<<2) = 40
>> Bitwise Right Shift (10>>2) = 2
>>> Bitwise Right Shift with Zero (10>>>2) = 2

JavaScript Logical Operators

Java script divides logic in two types, relation and condition. These functions are used to make the program flexible and powerful. They use and, or, not. Relational operators are used generally create loops.The following operators are known as JavaScript logical operators.

Operator Description Example
&& Logical AND (10==20 && 20==33) = false
|| Logical OR (10==20 || 20==33) = false
! Logical Not !(10==20) = true

JavaScript Assignment Operators

Under this you literally assign values to the variables like x= ravi, y= hello.The following operators are known as JavaScript assignment operators.

Operator Description Example
= Assign 10+10 = 20
+= Add and assign var a=10; a+=20; Now a = 30
-= Subtract and assign var a=20; a-=10; Now a = 10
*= Multiply and assign var a=10; a*=20; Now a = 200
/= Divide and assign var a=10; a/=2; Now a = 5
%= Modulus and assign var a=10; a%=2; Now a = 0

JavaScript Special Operators

These operators are used in special cases like: new, void, type of, comma, in delete etc.

Say for example delete command can be used to delete a particular property from a if you use delete it would delete a property from the given object.

The following operators are known as JavaScript special operators.

Operator Description
(?:) Conditional Operator returns value based on the condition. It is like if-else.
, Comma Operator allows multiple expressions to be evaluated as single statement.
delete Delete Operator deletes a property from the object.
in In Operator checks if object has the given property
instanceof checks if the object is an instance of given type
new creates an instance (object)
typeof checks the type of object.
void it discards the expression's return value.
yield checks what is returned in a generator by the generator's iterator.