Coding/Learn/javascript/logical and nullish
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnLogical Operators & Nullish Coalescing (??)
8 / 20 0%
Lesson 08 of 20
6 mins read

Logical Operators & Nullish Coalescing (??)

PreviousNext Lesson
In Plain English

Logical operators (&&, ||, !) combine conditions and short-circuit evaluation. The nullish coalescing operator (??) provides safe fallback values specifically for null and undefined (unlike || which treats 0 and "" as falsy).

The Mental Model

"The || operator is like a greedy filter that rejects anything empty (including 0 and empty strings). The ?? operator is a precision laser that only replaces actual missing items (null and undefined)."

Deep Dive: How It Works

Short-Circuiting: In a && b, if a is falsy, b is never evaluated. In a || b, if a is truthy, b is skipped.

Nullish Coalescing (??): Returns the right-hand operand only if the left-hand is null or undefined.

Optional Chaining (?.): Safely reads nested properties without throwing if an intermediate value is nullish: user?.profile?.avatar.

Syntax Blueprint

const value = input ?? "default_fallback"

Use ?? when 0 or "" are valid legitimate values that should not be overridden by a fallback.

PointCore Rules to Remember

Point star
&& (AND): Returns the first falsy operand, or the last operand if all are truthy.
Point star
|| (OR): Returns the first truthy operand, or the last operand if all are falsy.
Point star
?? (Nullish): Returns the right operand only if left is null or undefined.

Common Beginner Traps & How to Fix Them

Mistake:Using || for default numbers when 0 is valid.

Why it happens: const count = 0 || 10; sets count to 10 because 0 is falsy.

How to fix: Use ?? instead: const count = 0 ?? 10; correctly sets count to 0.

Pro Tip: Combine optional chaining and nullish coalescing for rock-solid object data extraction: const theme = config?.theme ?? "dark";

Live Interactive Example

Hit Run Code to see it live
Short-Circuit and Nullish Coalescing
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
11
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates why ?? is essential when dealing with legitimate 0 or empty string values.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Provide a safe fallback with ??

  • PointGiven let customTimeout = null;
  • Pointconst timeout = customTimeout ?? 3000;
  • PointPrint "Active Timeout: 3000 ms".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates