File tree Expand file tree Collapse file tree 2 files changed +36
-4
lines changed
Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ // operatorPrecedence.js
2+
3+ // INFO: Operator Precedence in JavaScript
4+
5+ /*
6+ Operator precedence determines the order in which parts of an expression are evaluated.
7+
8+ Expressions with multiple operators are evaluated based on this precedence,
9+ not just from left to right.
10+
11+ To avoid confusion and bugs, use parentheses () to explicitly specify the order.
12+
13+ ---
14+
15+ Example 1: Without parentheses (can be confusing)
16+ let score = 2 * 3 + 2 - 1;
17+ // Evaluated as: (2 * 3) + 2 - 1 = 6 + 2 - 1 = 7
18+
19+ ---
20+
21+ Example 2: With parentheses (clear and recommended)
22+ let score1 = ((2 * (3 + 2)) - 1);
23+ // Evaluated as: 2 * (5) - 1 = 10 - 1 = 9
24+
25+ ---
26+
27+ Console outputs:
28+ */
29+
30+ let score = 2 * 3 + 2 - 1 ;
31+ let score1 = ( ( 2 * ( 3 + 2 ) ) - 1 ) ;
32+
33+ console . log ( score ) ; // Output: 7
34+ console . log ( score1 ) ; // Output: 9
35+
36+ // TIP: Use parentheses to make your code easier to read and avoid unexpected results.
You can’t perform that action at this time.
0 commit comments