Short-circuit evaluation: Difference between revisions
→Boolean evaluation short-circuits by default in JavaScript
No edit summary |
|||
Line 1,314:
a
T or T = true</pre>
=={{header|JavaScript}}==
Short-circuiting evaluation of boolean expressions has been the default since the first versions of JavaScript.
<lang JavaScript>function a(bool) {
console.log('a -->', bool);
return bool;
}
function b(bool) {
console.log('b -->', bool);
return bool;
}
var x = a(false) && b(true),
y = a(true) || b(false);</lang>
The console log shows that each case (the binding of both x and y), only the left-hand part of the expression (the application of ''a(expr)'') was evaluated – ''b(expr)'' was skipped by logical short-circuiting.
<lang JavaScript>a --> false
a --> true</lang>
=={{header|jq}}==
|