In JavaScript, operator precedence determines the order in which operators are evaluated when an expression contains multiple operators. Operators with higher precedence are evaluated first, followed by operators with lower precedence.
Here’s a table showing the operator precedence in JavaScript, from highest to lowest:
Precedence | Operator |
17 | () |
16 | new, new.target |
15 | . (property access), [] (array element access), () (function call) |
14 | ++, — (postfix) |
13 | ++, — (prefix), +, –, !, ~, typeof, void, delete |
12 | ** (exponentiation) |
11 | *, /, % |
10 | +, – (binary addition/subtraction) |
9 | <<, >>, >>> (bitwise shifts) |
8 | <, <=, >, >=, in, instanceof |
7 | ==, !=, ===, !== |
6 | & |
5 | ^ |
4 | ` |
3 | && |
2 | ` |
1 | ? : (ternary conditional) |
0 | =, +=, -=, *=, /=, %=, &=, ^=, ` |
Note that operators with the same precedence are evaluated from left to right. For example, a + b – c is evaluated as (a + b) – c.
Understanding operator precedence is important when writing complex expressions, to ensure that the expression is evaluated in the intended order. If in doubt, use parentheses to explicitly specify the order of evaluation.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.