If * (multiplication) has higher precedence than /(Division) then why alert is not 1. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

If * (multiplication) has higher precedence than /(Division) then why alert is not 1.

Alert(2×2÷2×2);

4th Jul 2021, 6:20 AM
Dev Sharma
Dev Sharma - avatar
3 Antworten
+ 4
in javascript (and most of languages) multiplication has same precedence than division, so left to right associativity occurs ^^ to do multiplication before division, use parenthesis ;P
4th Jul 2021, 6:23 AM
visph
visph - avatar
+ 2
The expression is evaluated from left to right like this 2 * 2 -> (4) (4) / 2 -> (2) (2) * 2 -> (4)
4th Jul 2021, 6:35 AM
Ipang
+ 1
Thanks visph and Ipang
4th Jul 2021, 6:44 AM
Dev Sharma
Dev Sharma - avatar