Why the code returns this. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the code returns this.

var a = 5; a = a - a % 2 console.log(a) //4 Why this code returns 6 instead of 0

2nd Jun 2021, 4:59 AM
Coder-Rohit[{(∞)}]
Coder-Rohit[{(∞)}] - avatar
2 Answers
+ 3
when operators have same precedence, computation is done from left to right... but when one has higher precedence, it will be computed before ^^ to be aware of different operator precedence in JS, you could refer to this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
2nd Jun 2021, 5:12 AM
visph
visph - avatar
0
visph thanks again, you are genius
2nd Jun 2021, 7:31 AM
Coder-Rohit[{(∞)}]
Coder-Rohit[{(∞)}] - avatar