➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
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.
Operator | Operation | Order of Precedence | Order of Evaluation |
---|---|---|---|
++ | Increment | 1 | R -> L |
-- | Decrement | 1 | R -> L |
~ | Negation | 1 | R -> L |
! | NOT | 1 | R -> L |
*, /, % | Multiplication, division, modulus | 2 | L -> R |
+, – | Addition, subtraction | 3 | L -> R |
+ | Concatenation | 3 | L -> R |
<, <= | Less than, less than or equal to | 4 | L -> R |
>, >= | Greater than, greater than or equal to | 4 | L -> R |
== | Equal | 5 | L -> R |
!= | Not equal | 5 | L -> R |
=== | Identity | 5 | L -> R |
!== | Non-identity | 5 | L -> R |
&& | AND | 6 | L -> R |
|| | OR | 6 | L -> R |
?: | Ternary | 7 | R -> L |
= | Assignment | 8 | R -> L |
+=, -=, and so on. | Arithmetic assignment | 8 | R -> 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!