With short circuiting you can prevent the evaluation of part of an expression:
let x = "", y = 123;
x && alert("foo"); // ""
y || alert("bar") // 123
Since the logical ops form expressions you can use them in function invocations or return statements.
But ultimately, that's nothing more than conditional branching and can be easily reached with the ternary operator:
x ? alert("foo") : x; // ""
y ? y : alert("bar"); // 123
This is more readable and similarly concise. Is there a reason to utilize the short circuiting property of the logical operators except for the illustrative term?
&&
and||
were designed as special forms of&
and|
, optimised for particular cases. For people used to thinking in terms of boolean arithmetic,x ? y : x
forx && y
would be far less readable, as well as the side-effect issue and their not being quite like that in C from which Javascript inherited them. – Pedagogics