Logical
Logical operators combine or invert conditions. JavaScript has three classic ones, && (AND), || (OR) and ! (NOT), plus the nullish coalescing operator ??. You use them to check several requirements at once, to provide fallback values, and to run code only when something exists. They behave a little differently from logical operators in some other languages, because they return one of their operands rather than always returning a boolean.
AND, OR and NOT
a && bis truthy only when both sides are truthy.a || bis truthy when at least one side is truthy.!aflips truthiness:!trueisfalse,!""istrue. Two of them,!!value, convert any value to a real boolean.
const age = 22;
const hasId = true;
if (age >= 18 && hasId) {
console.log('Entry allowed');
}
const isWeekend = day => day === 'Sat' || day === 'Sun';
// They return an operand, not just true/false
console.log('Asha' && 'Ravi'); // "Ravi"
console.log('' || 'Default'); // "Default"
console.log(0 || 50); // 50
console.log(0 ?? 50); // 0
console.log(null ?? 50); // 50
// Logical assignment
const settings = { theme: '' };
settings.theme ||= 'light'; // '' is falsy, so becomes 'light'
settings.volume ??= 70; // only set if null/undefined
Short-circuit evaluation
JavaScript stops evaluating as soon as the result is known. With &&, if the left side is falsy, the right side never runs and the left value is returned. With ||, if the left side is truthy, the right side is skipped and the left value is returned. This is why user && user.logout() safely calls the method only when user exists. Today, optional chaining, user?.logout(), is often a clearer way to write that.
|| versus ?? for defaults
A long-standing habit is writing const qty = input || 1 to supply a default. The problem is that || replaces every falsy value, including 0, an empty string and false, which may all be valid input. If a user chooses quantity 0, it silently becomes 1. The ?? operator only falls back when the left side is null or undefined, so input ?? 1 keeps a real zero. Prefer ?? for defaults unless you truly want to treat every falsy value as missing.
Operator precedence
! binds tightest, then &&, then ||. So a || b && c means a || (b && c). Add parentheses whenever you mix them; it costs nothing and removes doubt. Mixing ?? directly with && or || without parentheses is a syntax error by design.
Optional chaining pairs naturally with ??. An expression such as user?.address?.city ?? 'Not provided' reads a nested value safely and supplies a label when any part of the path is missing.
A negation slip
Writing !a == b negates a first and then compares, which is rarely what was intended. Use a !== b. Similarly, reversing a combined condition needs care: the opposite of age >= 18 && hasId is age < 18 || !hasId, not age < 18 && !hasId. Swapping AND for OR when negating is known as De Morgan's law.
Main points
Use && and || to combine conditions, rely on short-circuiting for guarded calls, choose ?? when zero or empty strings are valid, and add parentheses when operators mix.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.