Skip to main content

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. 1

    Keep a zero

    easy10 min

    Use ?? so a volume of zero is not replaced by the default.

  2. 2

    Fill in a missing value

    easy10 min

    Use ?? to supply a default for a property that is not there.

  3. 3

    Show the difference

    easy10 min

    Show what || gives for a volume of zero.

  4. 4

    Combine with optional chaining

    easy10 min

    Read a missing nested city and fall back to a message.

  5. 5

    Default a function argument

    medium10 min

    Write a function that keeps a deliberate zero but defaults a missing value.

Find the bug(5)

  1. 1

    Fix the OR that ate the zero

    easy8 min

    A volume of zero is valid and should not be replaced by the default.

  2. 2

    Fix the empty string replaced

    easy8 min

    An empty name should be kept, not swapped for a default.

  3. 3

    Fix the single question mark

    easy8 min

    Nullish coalescing needs two question marks.

  4. 4

    Fix the false being replaced

    easy8 min

    A deliberate false should survive the default.

  5. 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