JavaScript Operators
Operators in JavaScript with examples:
Operator |
Operator |
Description |
Example |
Result |
Arithmetic |
+ |
Addition |
5 + 3 |
8 |
– |
Subtraction |
5 – 3 |
2 |
|
* |
Multiplication |
5 * 3 |
15 |
|
/ |
Division |
6 / 3 |
2 |
|
% |
Remainder (modulus) |
5 % 2 |
1 |
|
** |
Exponentiation |
2 ** 3 |
8 |
|
Assignment |
= |
Assignment |
x = 5 |
x is 5 |
+= |
Addition assignment |
x += 3 |
x = x + 3 |
|
-= |
Subtraction assignment |
x -= 2 |
x = x – 2 |
|
*= |
Multiplication assignment |
x *= 2 |
x = x * 2 |
|
/= |
Division assignment |
x /= 2 |
x = x / 2 |
|
%= |
Remainder assignment |
x %= 2 |
x = x % 2 |
|
**= |
Exponentiation assignment |
x **= 2 |
x = x ** 2 |
|
Comparison |
== |
Equal to |
5 == ‘5’ |
true |
=== |
Strict equal to (value and type) |
5 === ‘5’ |
false |
|
!= |
Not equal to |
5 != ‘5’ |
false |
|
!== |
Strict not equal to |
5 !== ‘5’ |
true |
|
> |
Greater than |
5 > 3 |
true |
|
< |
Less than |
5 < 3 |
false |
|
>= |
Greater than or equal to |
5 >= 5 |
true |
|
<= |
Less than or equal to |
5 <= 3 |
false |
|
Logical |
&& |
Logical AND |
true && false |
false |
|| |
Logical OR |
true || false |
true |
|
! |
Logical NOT |
!true |
false |
|
Bitwise |
& |
Bitwise AND |
5 & 1 |
1 (0101 & 0001) |
| |
Bitwise OR |
5 | 1 |
5 (0101 | 0001) |
|
^ |
Bitwise XOR |
5 ^ 1 |
4 (0101 ^ 0001) |
|
~ |
Bitwise NOT |
~5 |
-6 (bitwise NOT of 0101) |
|
<< |
Bitwise left shift |
5 << 1 |
10 (0101 << 1) |
|
>> |
Bitwise right shift |
5 >> 1 |
2 (0101 >> 1) |
|
>>> |
Bitwise unsigned right shift |
5 >>> 1 |
2 |
|
Ternary |
? : |
Ternary (conditional) operator |
true ? ‘yes’ : ‘no’ |
‘yes’ |
Type |
Typeof |
Returns the type of a variable |
typeof 42 |
“number” |
instanceof |
Checks if an object is an instance |
obj instanceof Array |
true if obj is an array |
Operator precedence
Here’s a comprehensive table that classifies JavaScript operators, provides examples, and includes their precedence and associativity:
Precedence |
Operator |
Description |
Example |
Result |
Associativity |
|
1 |
|
Grouping |
|
|
n/a |
|
2 |
|
Object Creation |
|
Creates a new |
n/a |
|
3 |
|
Member Access |
|
Accesses |
Left-to-right |
|
|
Computed Member Access |
|
Accesses first element |
Left-to-right |
||
4 |
|
Object Creation |
|
Creates a new |
Right-to-left |
|
5 |
|
Postfix Increment |
|
|
n/a |
|
|
Postfix Decrement |
|
|
n/a |
||
6 |
|
Prefix Increment |
|
|
Right-to-left |
|
|
Prefix Decrement |
|
|
Right-to-left |
||
|
Unary Plus |
|
Converts |
Right-to-left |
||
|
Unary Negation |
|
Negates |
Right-to-left |
||
|
Bitwise NOT |
|
Inverts bits of |
Right-to-left |
||
|
Logical NOT |
|
Inverts |
Right-to-left |
||
|
Type of |
|
Returns type of |
Right-to-left |
||
|
Discards return value |
|
Evaluates |
Right-to-left |
||
|
Deletes property |
|
Deletes |
Right-to-left |
||
7 |
|
Exponentiation |
|
|
Right-to-left |
|
8 |
|
Multiplication |
|
|
Left-to-right |
|
|
Division |
|
|
Left-to-right |
||
|
Remainder |
|
|
Left-to-right |
||
9 |
|
Addition |
|
|
Left-to-right |
|
|
Subtraction |
|
|
Left-to-right |
||
10 |
|
Bitwise Left Shift |
|
|
Left-to-right |
|
|
Bitwise Right Shift |
|
|
Left-to-right |
||
|
Bitwise Unsigned Right Shift |
|
|
Left-to-right |
||
11 |
|
Less Than |
|
|
Left-to-right |
|
|
Less Than or Equal To |
|
|
Left-to-right |
||
|
Greater Than |
|
|
Left-to-right |
||
|
Greater Than or Equal To |
|
|
Left-to-right |
||
|
Property in Object |
|
|
Left-to-right |
||
|
Instance of Object |
|
|
Left-to-right |
||
12 |
|
Equal |
|
|
Left-to-right |
|
|
Not Equal |
|
|
Left-to-right |
||
|
Strict Equal |
|
|
Left-to-right |
||
|
Strict Not Equal |
|
|
Left-to-right |
||
13 |
|
Bitwise AND |
|
|
Left-to-right |
|
14 |
|
Bitwise XOR |
|
|
Left-to-right |
|
15 |
` |
` |
Bitwise OR |
`5 |
1` |
|
16 |
|
Logical AND |
|
|
Left-to-right |
|
17 |
` |
` |
Logical OR |
`true |
||
18 |
|
Nullish Coalescing |
|
|
Left-to-right |
|
19 |
|
Conditional (Ternary) |
|
|
Right-to-left |
|
20 |
|
Assignment |
|
|
Right-to-left |
|
|
Addition Assignment |
|
|
Right-to-left |
||
|
Subtraction Assignment |
|
|
Right-to-left |
||
|
Multiplication Assignment |
|
|
Right-to-left |
||
|
Division Assignment |
|
|
Right-to-left |
||
|
Remainder Assignment |
|
|
Right-to-left |
||
|
Exponentiation Assignment |
|
|
Right-to-left |
||
|
Left Shift Assignment |
|
|
Right-to-left |
||
|
Right Shift Assignment |
|
|
Right-to-left |
||
|
Unsigned Right Shift Assignment |
|
|
Right-to-left |
||
|
Bitwise AND Assignment |
|
|
Right-to-left |
||
|
Bitwise XOR Assignment |
|
|
Right-to-left |
||
` |
=` |
Bitwise OR Assignment |
`x |
= 1` |
||
21 |
|
Comma |
|
|
Left-to-right |
- Associativity defines the order in which operators of the same precedence are processed.
Left-to-right means they are processed from left to right, and right-to-left means they are processed from right to left.
Let’s illustrate operator precedence with an example that combines several types of operators.
Example:
let result =
5 +
3 *
2 **
2 - (
6 /
3) &&
4 ||
false;
Let’s break down this
expression step by step according to operator precedence:
1. Grouping (()
):
-
- Evaluate
(6 / 3)
first because of parentheses. - Result:
2
- Evaluate
2. Exponentiation (**
):
-
- Evaluate
2 ** 2
next. - Result:
4
- Evaluate
3. Multiplication (*
):
-
- Evaluate
3 * 4
next. - Result:
12
- Evaluate
4. Addition and Subtraction (+
, -
):
-
- Evaluate
5 + 12
next. - Result:
17
- Then evaluate
17 - 2
. - Result:
15
- Evaluate
5. Logical AND (&&
):
-
- Evaluate
15 && 4
. - Result:
4
(since15
is truthy, the result is4
)
- Evaluate
6. Logical OR (||
):
-
- Evaluate
4 || false
. - Result:
4
(since4
is truthy, the result is4
)
- Evaluate
So, the final result of the expression let result = 5 + 3 * 2 ** 2 - (6 / 3) && 4 || false;
is 4
.