Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Nullish Default (??)

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 114: The Nullish Coalescing Operator (??)

Implement `nullishDefault(value, fallback)` returning `value` unless it is null or undefined, using `??`. Contrast this with `||`.

Sample tests

Input: nullishDefault(0, 5)
Output: 0
Input: nullishDefault(null, 5)
Output: 5

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Unlike ||, ?? keeps 0 and empty string.
Visualize this concept: Optional Chaining and Nullish Coalescing →
Approach & explanation (try first)

Nullish coalescing preserves valid falsy values, only substituting for null or undefined.

Loading...
⌘/Ctrl + Enter

Run your code to see results.