Use ?? so a volume of zero is not replaced by the default.
Acceptance criteria
- 1.#out should show 0.
JavaScript Tutorial · JavaScript Objects
These 10 tasks go with the JavaScript Nullish Coalescing chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “The JavaScript nullish coalescing operator gives a fallback only when a value is null or undefined.” .
1.Keep a zeroexercise · easy · 10 min
Use ?? so a volume of zero is not replaced by the default.
Passes when: #out should show 0.
2.Fill in a missing valueexercise · easy · 10 min · practice plan
Use ?? to supply a default for a property that is not there.
Passes when: #out should show medium.
3.Show the differenceexercise · easy · 10 min · practice plan
Show what || gives for a volume of zero.
Passes when: #out should show 50.
4.Combine with optional chainingexercise · easy · 10 min · practice plan
Read a missing nested city and fall back to a message.
Passes when: #out should show no city.
5.Default a function argumentexercise · medium · 10 min · practice plan
Write a function that keeps a deliberate zero but defaults a missing value.
Passes when: #out should show 0 then 50.
6.Fix the OR that ate the zerobug fix · easy · 8 min
A volume of zero is valid and should not be replaced by the default.
Passes when: #out should show volume 0.
7.Fix the empty string replacedbug fix · easy · 8 min · practice plan
An empty name should be kept, not swapped for a default.
Passes when: #out should show kept the empty value.
8.Fix the single question markbug fix · easy · 8 min · practice plan
Nullish coalescing needs two question marks.
Passes when: #out should show medium.
9.Fix the false being replacedbug fix · easy · 8 min · practice plan
A deliberate false should survive the default.
Passes when: #out should show false.
10.Fix the missing bracketsbug fix · easy · 8 min · practice plan
?? cannot be mixed with || without brackets to say which runs first.
Passes when: #out should show yes.
Between them these tasks cover Nullish coalescing, Default values and Falsy values.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.