What is an operator?
Consider a simple expression 2 + 5 equals to 7. Here, 2 and 5 are called operands and the â€˜+â€™ is called operator. JavaScript supports the following 5 types of operators:
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
Arithmetic Operators
JavaScript supports the following arithmetic operators âˆ’
Lets take tow variable A= 10 B = 20, then âˆ’
Sr.No | Operator and Description |
---|---|
1 | + (Addition) Adds two operands Ex: A + B will give 30 |
2 | – (Subtraction) Subtracts the second operand from the first Ex: A – B will give -10 |
3 | * (Multiplication) Multiply both operands Ex: A * B will give 200 |
4 | / (Division) Divide the numerator by the denominator Ex: B / A will give 2 |
5 | % (Modulus) Outputs the remainder of an integer division Ex: B % A will give 0 |
6 | ++ (Increment) Increases an integer value by one Ex: A++ will give 11 |
7 | — (Decrement) Decreases an integer value by one Ex: A– will give 9 |
Note âˆ’ Addition operator (+) can you to add two numbers and also to concat strings . e.g. “a” + 10 will give “a10”.
Comparison Operators
Here is the complete set of JavaScript comparison operators:
(Letâ€™s assume variable a=100 and variable b=200)
Sr No | Operators and Description |
---|---|
1 | `= = (Equals) Checks whether the values of two operands are equal or not, when yes, then the condition becomes true. Ex: (a==b) is not true. |
2 | != (Not Equal) Checks whether the values of two operands are equal or not, if the values are equal, then the condition becomes false. Ex: (a!=b) is true. |
3 | > (Greater than) Compare two numbers if first is greater than second then return true. Ex: (a > b) is not true. |
4 | < (Less than) Compare two numbers if first is less than second then return true. Ex: (a < b) is true. |
5 | >= (Greater than or Equal to) Checks if the value of the left operand/expression is greater than or equal to the value of the right operand, when yes, the condition becomes true. Ex: (a >= b) is not true here. |
6 | <= (Less than or Equal to) It Checks whether the value of the left operand/expression is less than or equal to the value of the right operand, when yes, the condition becomes true. Ex: (a <= b) is true here. |
Logical Operators
Here is the complete set of JavaScript logical operators:
(Letâ€™s assume variable A=100 and variable B=200)
Sr No | Operators and Description |
---|---|
1 | &&(Logical AND) When both the operands are non-zero, the condition is true. Ex: (a&&b) is true. |
2 | || (Logical OR) When any of the two operands are non-zero, the condition becomes true. Ex: (a||b) is true. |
3 | !(Logical NOT) Reverses the state of the operand. When a condition is true, then the Logical NOT operator will make it false and vice-versa. Ex: (!a) is false. |
Bitwise Operators
Here is the complete set of JavaScript bitwise operators:
(Letâ€™s assume variable a=2 and b=3)
Sr No | Operators and Description |
---|---|
1 | & (Bitwise AND) It performs Boolean AND operation on each bit of the variable placed after it. Ex: (a&b) is 2. |
2 | | (BitWise OR) It performs Boolean OR operation on each bit of the integer. Ex: (a | b) is 3. |
3 | ^ (Bitwise XOR) It performs Boolean exclusive OR operation on each bit of the integer. XOR means either of the operands is true, but not both. Ex: (a^b) is 1. |
4 | ~ (Bitwise Not) This unary operator operates by reversing all the bits of the operand. Ex: (~b) is -4 here. |
5 | << (Left Shift) It moves all the bits in its first operand to the left by the number of places specified in the right side expression/operand. New bits are filled with zeroes. Shifting a value left by one position is equivalent to multiplying it by 2. Ex: (a << 1) is 4. |
6 | >> (Right Shift) The left operandâ€™s value is shifted to the right by the number of bits specified by the right operand/expression. Ex: (a >> 1) is 1. |
7 | >>> (Right shift with Zero) Itâ€™s same as the >> operator, except that the bits shifted in on the left are always zero. Ex: (a >>> 1) is 1. |
Assignment Operators
Here is the complete set of JavaScript assignment operators:
Sr No | Operators and Description |
---|---|
1 | = (Simple Assignment) It assigns values from the right side operand/expression to the left side operand Ex: c= a + b assigns the value of (a+b) into c |
2 | += (Add and Assignment) It adds the right operand/expression to the left operand and assigns the final result to the left operand. Ex: c+= a is same as c = c + a |
3 | âˆ’= (Subtract and Assignment) It subtracts the right operand/expression from the left operand and assigns the final result to the left operand. Ex: c-= a is same as c=c – a |
4 | *= (Multiply and Assignment) It multiplies the right operand/expression with the left operand and assigns the final result to the left operand. Ex: c*=a is same as c = c*a |
5 | /= (Divide and Assignment) It divides the left operand with the right operand/expression and assigns the final result to the left operand. Ex: c /=a is same as c = c/a |
6 | %= (Modules and Assignment) It takes modulus using two operands/expressions and assigns the final result to the left operand. Ex: c %= a is same as c=c % a |
Note: Same logic applies to Bitwise operators also. Theyâ€™ll become <<=, >>=, >>=, &=, |=, ^=.
Conditional Operator(? : )
It is short form of an if-else statement. This operator first checks whether an expression is true or false and then executes one of the two given statements depending upon the result of the evaluated condition i.e., true or false.
Sr No | Description |
---|---|
1 | ? : (Conditional) Is condition true? Then value is a : otherwise value becomes b |
Special Operators
The unary type operator typeof operator is placed just before its single operand, which can be of any datatype.
It determines the datatype of its argument. It returns string. Here is the complete list of the return values for the typeof Operator:
Sr No | Type | String Returned by typeof |
---|---|---|
1 | Number | number |
2 | String | string |
3 | Boolean | boolean |
4 | Object | object |
5 | Function | function |
6 | Undefined | undefined |
7 | Null | object |