Nearby lessons

7 of 22

Advanced JavaScript: Nullish Coalescing Operator (??)

📌 What is Nullish Coalescing Operator?

The nullish coalescing operator ?? is a logical operator that returns the right-hand operand when the left-hand operand is null or undefined. It's useful when providing default values without incorrectly handling falsy values like 0, "", or false.

1️⃣ Basic Usage of ??

Use ?? to assign default values when a variable is null or undefined.

  • Returns the left-hand value if it is not null or undefined
  • Otherwise, returns the right-hand (default) value
  • Does NOT treat falsy values like 0, '', or false as nullish
Code Example
PREVIEW READY
Loading Editor...
Live Preview

2️⃣ Difference between || and ??

|| returns the right-hand operand if the left is any falsy value (like 0, "", false), while ?? only checks for null or undefined.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ Chaining with ??

Chain ?? to provide fallback values in sequence.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4️⃣ Invalid Mixing with && or ||

Mixing ?? with || or && without parentheses causes a syntax error. Always use parentheses when mixing.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - Object Literals