JavaScript Operator Precedence

JavaScript Operator Precedence | Operator precedence describes the order in which operators are processed when a statement has multiple operators performing different activities. It is crucial to guarantee the right outcome and aid the compiler in comprehending the proper order of actions. Higher-priority operators are addressed first. The priority, and therefore the likelihood of their resolution, lowers as one moves down the list.

Priority and Associativity: In general, associativity states that the outcome of an operation is unaffected by the order of the operands. The compiler can be instructed on which operations to carry out first using precedence. Take the numbers 2, 3, and 4 as an example. Now think about these two actions:

( 1 + 3 ) + 4 = 5 + ( 3 + 9 )
( 1 >= 2 ) or ( 3 != 5)

Associativity is the first operation, where the order is irrelevant. In the second scenario, known as precedence, there must be an appropriate order for the operations to be carried out in order to achieve the intended outcome. Also see:- JavaScript Order of Operations

While dealing with precedence operations requires either left-to-right or right-to-left associativity, associativity is not a single concept. This determines the direction the operation should start and fully depends on the operation.

Operator Precedence Table: This table shows the order in which operators are given precedence over one another. The precedence of these operators declines as one moves down the table, meaning that an operator’s priority is lower than those above it and greater than those below it. The priority of the operators in the same row is the same.

JavaScript Operator Precedence Table

The highest precedence in this table is 1, while the lowest precedence is 19. You do not have to memorize this table, rather whenever there is a requirement visit this page and have a look at the table.

OperatorOperationOrder of PrecedenceOrder of Evaluation
++Increment1R -> L
--Decrement1R -> L
~Negation1R -> L
!NOT1R -> L
*, /, %Multiplication, division, modulus2L -> R
+, –Addition, subtraction3L -> R
+Concatenation3L -> R
<, <=Less than, less than or equal to4L -> R
>, >=Greater than, greater than or equal to4L -> R
==Equal5L -> R
!=Not equal5L -> R
===Identity5L -> R
!==Non-identity5L -> R
&&AND6L -> R
||OR6L -> R
?:Ternary7R -> L
=Assignment8R -> L
+=, -=, and so on.Arithmetic assignment8R -> L

The equal == and identity equal === may look similar but they work very differently. Learn more here:- JavaScript Double Equals Vs Triple Equals (== vs ===) Operator

JavaScript Operator Precedence Example

let num = 2 + 2 / 2;
console.log(num);

Output:-

3

The order of precedence of the division (/) operator is lesser than the addition (+) operator therefore first division operation will be performed and after that, the addition operation will be performed. So, 2 + 2 / 2 = 2 + 1 = 3. Note that it is following the BODMAS rule.

let num = (15 * 32 / 2 * 5) / 75;
console.log(num);

Output:-

16

In the above equation ( ) will be executed first. The order of precedence for division and multiplication operators are same. Therefore they will be executed based on the order of evaluation which is L -> R means left to right.

(15 * 32 / 2 * 5) / 75
= (480 / 2 * 5) / 75
= (240 * 5) / 75
= 1200 / 75
= 16

let num = 8 / 4 * (6 + 2 * 4) + 32 - 2;
console.log(num);

Output:-

58

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *