Nearby lessons
7 of 22Advanced 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
nullorundefined - Otherwise, returns the right-hand (default) value
- Does NOT treat falsy values like 0, '', or false as nullish
Example02
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.
Example03
3️⃣ Chaining with ??
Chain ?? to provide fallback values in sequence.
Example04
4️⃣ Invalid Mixing with && or ||
Mixing ?? with || or && without parentheses causes a syntax error. Always use parentheses when mixing.
Example05
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3