Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Lesson

Logical Operators

Logical Operators 12 min&& returns the first falsy value or the last value if all are truthy, || returns the first truthy value, and ?? works like || but only treats null and undefined as empty, not every falsy value.

Logical Operators

In short: && and || don't just return true/false — they return one of their actual operands, and both stop reading as soon as they know the answer (called short-circuiting). && returns the first falsy value it meets, or the last value if every one is truthy. || returns the first truthy value it meets. ?? behaves like ||, but only treats null/undefined as empty — not 0, "", or false, which || would also skip past.

Two gate chains: user && user.plan && user.plan.active stopping at user.plan → undefined (the rest never evaluated); nickname || realName || "Guest" stopping at realName → "Ana" (first truthy value) — with a note that ?? works like || but only skips null/undefined

Safely read a nested property

&& chains are a common guard against reading a property of something that might not exist — it stops the instant something is falsy:

javascript

This prints undefined — user is truthy, so evaluation continues to user.plan, which is undefined (falsy). && stops right there and returns that undefined without ever touching user.plan.active, which would have thrown trying to read .active off undefined.

Pick the first name that's actually there

|| walks left to right and returns the first truthy value — perfect for a chain of fallbacks:

javascript

nickname is "", falsy, so || moves on. realName is "Ana", truthy — || stops there and returns it, never even looking at "Guest".

The trap: a real zero treated as missing

|| and ?? only disagree on one thing — what counts as "empty" — and that difference matters the moment 0 is a genuine value, not an absence:

javascript

0 ?? "default" is 0 — ?? only checks for null/undefined, and 0 is neither, so it's kept. 0 || "default" is "default" — || treats 0 as falsy and replaces it, even though 0 might be exactly the right answer (an empty cart, a muted volume, zero items in stock). Use ?? when 0, "", or false are meaningful values you want to keep.

Quick practice

quick drill

What does const user = { plan: undefined }; console.log(user && user.plan && user.plan.active); print?

quick drill

What does "" || "Ana" || "Guest" evaluate to?

quick drill

What is 0 ?? "default"?

quick drill

What is 0 || "default"?

quick drill

Which best describes short-circuiting in && and ||?

🎯 Your turn

A product page shows stock || "Out of stock" to display how many are left. Trace this against stock = 0 (genuinely zero left), then trace stock ?? "Out of stock" against the same value, and explain why ?? is the better choice here.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainStock count zero

A product page shows stock || "Out of stock" to display how many are left, but an item with stock = 0 (genuinely out of stock) instead shows the number 0 replaced by nothing useful, or an item with stock = 5 shows 5 fine. Actually: explain why stock ?? "Out of stock" is the better choice when stock can legitimately be 0, tracing both operators against stock = 0.