Related to js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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. https://code.sololearn.com/WtvgkIgIrt3J/?ref=app

26th Oct 2021, 1:39 PM
Himansh
Himansh - avatar
4 Answers
+ 3
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.
26th Oct 2021, 1:44 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 2
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.
26th Oct 2021, 1:46 PM
Valen.H. ~
Valen.H. ~ - avatar
0
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.
28th Oct 2021, 6:02 AM
Himansh
Himansh - avatar
0
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.
28th Oct 2021, 6:07 AM
Himansh
Himansh - avatar