Skip to content

Commit c16fbf3

Browse files
feat: add operatorPrecedence.js
1 parent 2e0a789 commit c16fbf3

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

part1 (Basics)/08_opePre.js

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.

0 commit comments

Comments
 (0)