JavaScript Nullish Coalescing — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Nullish Coalescing lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Keep a zero
easy10 minUse ?? so a volume of zero is not replaced by the default.
- 2
Fill in a missing value
easy10 minUse ?? to supply a default for a property that is not there.
- 3
Show the difference
easy10 minShow what || gives for a volume of zero.
- 4
Combine with optional chaining
easy10 minRead a missing nested city and fall back to a message.
- 5
Default a function argument
medium10 minWrite a function that keeps a deliberate zero but defaults a missing value.
Find the bug(5)
- 1
Fix the OR that ate the zero
easy8 minA volume of zero is valid and should not be replaced by the default.
- 2
Fix the empty string replaced
easy8 minAn empty name should be kept, not swapped for a default.
- 3
Fix the single question mark
easy8 minNullish coalescing needs two question marks.
- 4
Fix the false being replaced
easy8 minA deliberate false should survive the default.
- 5
Fix the missing brackets
easy8 min?? cannot be mixed with || without brackets to say which runs first.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace