Related to js | Sololearn: Learn to code for FREE!


Related to js

The precedence of && operator is greater than || operator, hence in following statement, && should be evaluated first, if it is, then why alert is not working as && finds first falsy value and alert evaluates to false.

10/26/2021 1:39:04 PM


4 Answers

New Answer


Greater precedence means that the statements are captured with the following grouping/order: (x > 0) || (alert("") && "hii") So the short-circuit happens first, as anyone would want to.


Some languages have short circuit, they evaluate from left to right to skip unneeded steps. If OR had greater precedence, the same would be evaluated first but the actual computation would be: (x > 0 || alert("")) && ("hii") AND also short-circuits on first failure instead of success.


Valen.H. ~ So as you said in first comment that && shoukd be executed first then the result should is execution of alert function as && operator finds first falsy value and alert function will evaluate to false.


If i set the value of x > 0 to x > 1, then alert function is executed, mening that, || operator is executing first regardless of higher precedence of && operator.