Logical Operators
EasyIn plain terms
&& (AND) returns the first falsy or last value. || (OR) returns the first truthy or last value. ! (NOT) inverts to boolean. Use for conditionals and short-circuit evaluation.
What you need to know
- •&& AND, || OR, ! NOT
- •Short-circuit evaluation
- •Return original values, not just boolean
Try it yourself
Copy the code below and run it in your browser console or a code editor:
true && true; // true
true && false; // false
null || 'default'; // "default" (first truthy)
'' || 'fallback'; // "fallback"
!true; // false
!!'hi'; // true (double NOT for boolean)
// Short-circuit
const user = null;
const name = user && user.name; // null (no error)Learn more
Dive deeper with these trusted resources: