What's the result of OR'ed alerts?
importance: 3
What will the code below output?
alert( alert(1) || 2 || alert(3) );
The answer: first 1, then 2.
alert( alert(1) || 2 || alert(3) );
The call to alert does not return a value. Or, in other words, it returns undefined.
- The first OR
||evaluates its left operandalert(1). That shows the first message with1. - The
alertreturnsundefined, so OR goes on to the second operand searching for a truthy value. - The second operand
2is truthy, so the execution is halted,2is returned and then shown by the outer alert.
There will be no 3, because the evaluation does not reach alert(3).