Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 156 Β· Operators

Logical Operators and Short-Circuiting

Combine conditions with JavaScript's &&, || and ! operators, learn how short-circuit evaluation works, set defaults, and use ||=, &&= and ??= assignment.

Real decisions rarely depend on a single condition. A user can check out if the cart is not empty and they are signed in. A field is invalid if it's empty or too long. Logical operators let you combine conditions like this: && (and), || (or) and ! (not).

In JavaScript they do more than combine true and false. && and || return one of their operands β€” not necessarily a boolean β€” and they stop evaluating as soon as the answer is known. This behavior, called short-circuiting, powers several everyday idioms: default values, guards and conditional calls. This lesson covers all of it, plus the logical assignment operators ||=, &&= and ??=.

&&, || and ! with booleans

With real booleans the three operators work like the words they stand for:

Expression Result
a && b true only if both are true
a || b true if at least one is true
!a the opposite of a
JS Combining conditions

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The operators have different precedence: ! binds tightest, then &&, then ||. So a || b && c means a || (b && c). Just as with arithmetic, add parentheses whenever you mix && and || β€” the reader will thank you.

Negating combined conditions: "not (A and B)" is the same as "(not A) or (not B)", and "not (A or B)" is "(not A) and (not B)". These are De Morgan's laws, and they help you rewrite a confusing !(a && b) into something that reads naturally.

Short-circuit evaluation

JavaScript evaluates && and || from left to right and stops as soon as the result is decided:

  • a && b β€” if a is falsy, the whole thing must be false, so b is never evaluated.
  • a || b β€” if a is truthy, the whole thing must be true, so b is never evaluated.

You can watch this happen by putting functions with side effects on the right:

JS Watching short-circuiting

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the first and third cases, "right" is never printed. This isn't just an optimization β€” it's a guarantee you can rely on, and it's what makes the idioms below safe.

&& and || return a value, not just a boolean

Here's the part that makes JavaScript different from many languages. && and || don't convert their result to a boolean. They return whichever operand decided the result:

  • a || b returns a if a is truthy, otherwise b.
  • a && b returns a if a is falsy, otherwise b.
JS What && and || return

Press Run (or Ctrl + Enter) to see the console output here.

No output.

When the values are already booleans, this makes no difference. With other values, it enables patterns you'll see everywhere. The truthiness rules from Booleans and Truthiness decide which value is "truthy" or "falsy".

Idiom: default values with ||

|| gives you a quick fallback when a value is missing:

JavaScript
function greet(name) {
  const displayName = name || "guest";
  return `Welcome, ${displayName}!`;
}

greet("Ines");  // "Welcome, Ines!"
greet("");      // "Welcome, guest!"
greet();        // "Welcome, guest!"

But || replaces every falsy value, including legitimate ones like 0, "" and false:

JS The || default trap

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The nullish coalescing operator ?? is the fix: it only falls back when the left side is null or undefined. In modern code, prefer ?? for defaults and keep || for when every falsy value genuinely means "use the fallback" (like an empty search box). ?? is the star of the next lesson, Nullish Coalescing and Optional Chaining.

Idiom: guards with &&

Because && stops at the first falsy value, it can guard an expression that would otherwise fail:

JavaScript
const user = getUser();       // might return null

// Only read .name if user exists
const name = user && user.name;

// Only call the function if it exists
options.onSave && options.onSave();

If user is null, the first line stores null in name and never touches user.name, so nothing crashes. If options.onSave is missing, the call on the second line simply doesn't happen.

You'll see this in older code and in UI templates. For reading properties, optional chaining (user?.name) has largely replaced it. And for running code conditionally, a plain if is usually clearer than condition && doSomething() β€” save the short form for very small cases.

Logical assignment: ||=, &&= and ??=

Three operators combine a logical check with assignment. Each only assigns when the short-circuit rule says the right side would be used:

Operator Assigns when the variable is… Same as
a ||= b falsy a || (a = b)
a &&= b truthy a && (a = b)
a ??= b null or undefined a ?? (a = b)
JS Logical assignment operators

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last example is a neat trick: groups.fruit ??= [] creates the array the first time and returns the existing one afterward, so you can push into it immediately. The parentheses are needed because the assignment must happen before .push() is called on its result.

??= is the one you'll reach for most: "give this a default if it isn't set yet." Like the other logical operators, these short-circuit β€” if no assignment is needed, the right side isn't even evaluated. They're supported in all modern browsers and Node.js since 2020.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. What does "" || "hello" return?

    || returns the first truthy operand. The empty string is falsy, so it moves on to "hello".
  2. 2. What does 0 && expensiveCall() do?

    0 is falsy, so && short-circuits and returns it. The right side never runs.
  3. 3. A user set volume to 0. Which keeps that choice while defaulting missing values to 50?

    ?? only falls back for null and undefined. || and the ! check treat 0 as missing.
  4. 4. How is a || b && c evaluated?

    && has higher precedence than ||. Use parentheses to make mixed expressions clear.
  5. 5. After let x = ""; x ||= "none";, what is x?

    ||= assigns when the variable is falsy, and an empty string is falsy.

Summary

  • && (and), || (or) and ! (not) combine conditions; ! binds tightest, then &&, then ||.
  • && and || short-circuit: the right side runs only if it's needed.
  • They return one of the operands: || gives the first truthy value, && the first falsy value (or the last value).
  • x || fallback replaces every falsy value; x ?? fallback replaces only null and undefined.
  • ||=, &&= and ??= assign conditionally; ??= is the go-to way to set a default.

Next: Nullish Coalescing and Optional Chaining β€” ?? and ?. for working safely with missing values.