About
Logical operator in Javascript
Articles Related
List
- &&: AND
OR, ||
Returns expr1 if it can be converted to true; otherwise, returns expr2. See Javascript - Boolean (Truthy|Falsy)
expr1 || expr2
Usage Example:
- Initialization even if it's a null object
x = null;
y = x || "x was null";
console.log(y)
Snippet example:
Example:
console.log('Return expression1')
console.log(true || 'expr2')
console.log(1 || 'expr2')
console.log('s' || 'expr2')
console.log('\n')
console.log('Return expression2')
console.log(false || 'expr2')
console.log(0 || 'expr2')
console.log('' || 'expr2')
&&
- true && expression always evaluates to expression
- false && expression always evaluates to false
Therefore, in a statement such as condition && expression, if the condition is true, the element right after && will appear in the output. If it is false, it will be ignored.