Javascript boolean operators
The and-operator (&&) and the or-operator (||) employ short-circuiting to prevent unnecessary work if the outcome of the operation does not change with the extra work.
In x && y, y will not be evaluated if x evaluates to false, because the whole expression is guaranteed to be false.
In x || y, y will not be evaluated if x evaluated to true, because the whole expression is guaranteed to be true.
Example
function T() { // True
console.log("T");
return true;
}
function F() { // False
console.log("F");
return false;
}
Example1
T() && F();
Output
'T'
'F'
'F'
Example 2
F() && T();
Output
'F'
Example 3
T() || F();
Output
'T'
Example 4
F() || T();
Output
'F'
'T'
'T'