With a short circuit, you can prevent the evaluation of part of the expression:
let x = "", y = 123;
x && alert("foo");
y || alert("bar")
Since logical expressions are ops, you can use them in function calls or in return statements.
But, ultimately, this is nothing more than conditional branching and can be easily achieved using the ternary operator:
x ? alert("foo") : x; // ""
y ? y : alert("bar"); // 123
It is more readable and similarly concise. Is there any reason to use the short circuit property of logical operators, with the exception of the illustrative term?
source
share