Learn Operators in Javascript

Operators in javascript are the characters used in javascript expressions to perform arithmetic, comparison, logical, and assignment operations among operands.

Don’t you understand? Don’t worry, I will give one real-life example, to better understand.

Let’s assume, that you went to a doctor for treatment, how does he treat you? Does he treat you with his bare hands? No. He uses some equipment to treat you like a stethoscope, syringe, thermometer, etc.. In the above example, the doctor is a computer and treatment is an operation and equipment are the operators.

How the doctor used equipment to treat a patient likewise javascript also uses some operators to perform different types of operations.

For example `var x = 5 * 4;`   Here `*` is the operator, and `5, 4` is operands.

In the above example,  `*` operator multiply two numbers and stores the value in variable  `x`

There is a bunch of operators in javascript and those are categorized into different types.

1. Arithmetic Operators
2. Comparison Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators

Ok, Let’s discuss these operators in detail one by one.

1.  Arithmetic Operators in Javascript

Below are the list of arithmetic operators in javascript.

Operator Operation Example
`+` Plus (perform addition operation on two or more operands) `2 + 2 = 4; && 4 + 5 + 6 = 15;`
`-` Minus (Perform subtraction operation on two or more operands) `5 - 3 = 2; && 10 - 20 - 30 = -40;`
`*` Multiply (Perfrom multiplication operation on two or more operands) `5 * 4 = 20; && 4 * 2 * 2 = 16;`
`/` Divison (Perfrom divison operation on two or more operands) `10 / 5 = 2; && 10 / 2 / 2 = 2.5;`
`%` Modulus (Perform Divison operation on operands and results remainder only) `10 % 5 = 0;`
`++` Increment (Increment the value of operand by one time) `2++ = 3; && ++2 = 3;`
`--` Decrement (Decrement the value of operand by one time) `5-- = 4; && --5 = 4;`

2. Comparison Operators in Javascript

Below are the list of comparison operators in javascript.

Operator Operation Example
`==` Equal to (Evaluates true if the left operand is equal to the right operand) `4 == 4 --> true; && 4 == ['4'] --> true;`
`===` Exact Equal to (Evaluates true if the left operand is exactly equal to the right operand) `4 === ['4'] --> false; && 4 === 4 --> true;`
`!=` Not Equal to (Works opposite to the == operator) `4 != ['4'] --> false; && 4 != 5 --> true;`
`!==` Not Exactly Equal to (Works opposite to the === operator) `4 !== ['4'] --> true; && 4 !== 4 --> false;`
`<` Less than (Evaluates true if the left operand is less than the right operand) `5 < 10 --> true; && 5 < 3 --> false;`
`<=` Less than equal to (Evaluates true if the left operand is less than or equal to the right operand) `5 < 5 --> false; && 5 <= 5 --> true;`
`>` Greater than (Evaluates true if the left operand is greater than to the right operand) `5 > 5 --> false; && 5 > 3 --> true;`
`>=` Greater than or equal to (Evaluates true if the left operand is greater than or equal to the right operand) `3 > 3 --> false; && 3 >= 3 --> true;`

3. Assignment Operators in Javascript

Below are the list of assignment operators.

Operator Operation Example
`=` Assignment (Assign the value of right operand to the left operand) `var x = y;`
`+=` Addition assignment (Perform addition operation on left and right operands and stores the value on left operand) `var a = 5;`
`a += 5; //returns 10;`
`-=` Subtract assignment (Perform subtraction operation on left and right operands and stores the value on left operand) `var a = 5;`
`a -= 5; //returns 0;`
`*=` Multiply assignment (Perfrom multiplication operation on left and right operands and stores the value on the left operand) `var a = 5;`
`a *= 3; //returns 15;`
`/=` Divison assignment (Perfrom divison operation on both operands and stores the value on the left operand) `var a = 10;`
`a /= 2; //returns 5;`
`%=` Modulus assignment (Perform modulus operation on both operands and stores result on the left operand) `var a = 5;`
`a %= 2; //returns 1;`

4. Bitwise Operators in Javascript

Below are the list of bitwise operators used in javascript.

Operator Operation Example
`&` AND (Performs bitwise and operation on two operands) `3 & 1 -- > 1`
`|` OR (Performs bitwise or operation on two operands ) `5 | 2 -- > 7`
`^` XOR (Performs bitwise xor operation on two operands) `2 ^ 1 --> 3`
`~` NOT (Performs bitwise not operation on single operand) `~9 --> 10`
`<<` Zero fill left shift (Performs bitwise left shift operation on two operands) `3 << 1 --> 6`
`>>` Signed right shift (Performs bitwise signed right shift operation on two operands) `4 >> 1 --> 2`
`>>` Zero fill right shift (Performs bitwise zero fill right shift operation on two operands) `4 >>> 10 --> 0`

5. Logical Operators in Javascript

Below are the list of logical operators.

Operator Operation Example
`&&` and (Combines two expressions and evaluates true when both the expressions are true) `5>1 && 2< 1 --> false; 5>1 && 2< 3 --> true;`
`||` or (Combines two expressions and evaluates true when any of the expression is true) `5 | 2 -- > 7`
`!` not (Negate the value of evaluated expression) `2 > 1 --> true; && !(2 > 1) --> false;`

6. Special Operators in Javascript

Below are the list of important special operators.

Operator Operation Example
`delete` deletes a particular item `var user = {name:'jhon',age:35};`
`delete user.name; // return {age:35};`
`typeof` returns the type of a given value `typeof("jhon doe"); // return "string"; && typeof(25); //return "number";`
`?:` Ternary operator – Assigns a value to a variable based on given condition `(20 > 9) ? "pass" : "fail" // returns pass`