+ 1
3 not alert in this code. Why ??
Is that how behaves 'or' logic ?? https:// https://code.sololearn.com/Wgz3SFSfWDJL/?ref=app /Wgz3SFSfWDJL/?ref=app
2 Answers
+ 4
alert(alert(1) || 2 || alert(3))
First, expression inside parenthesis is evaluated before used as argument of the alert() outer call...
alert(1) open a dialog box for displaying "1", and then return "undefined" ( because doesn't return any value ), so alert(1) is evaluated as "false" regarding the "or" logical expression...
2 is evaluated as "true", so as soon as a "true" value is encountered in an "or" logical expression, next part is not required to be evaluated to know the result of the whole logical expression ( "true" if at least one condition is "true" ), so outer alert() call is done with the "2" value ( the first "true" condition ), opening a second dialog box for displayig "2" ( first alert() was done before ).
0
The 2 is not wrapped in an alert method, so the outside alert method will think that this is the last thing to be output, and thus ignores the last alert message. This will alert 3 as intended:
alert(alert(1) || alert(2) || alert(3))