Skip to main content

JavaScript null and undefined — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript null and undefined 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

    Report an unassigned variable

    easy10 min

    Report the value of a variable that was declared but not set.

  2. 2

    Report a missing property

    easy10 min

    Read a property that does not exist and report it.

  3. 3

    Empty a value on purpose

    easy10 min

    Set a variable to null and report it.

  4. 4

    Check for either

    easy10 min

    Use one check that catches both null and undefined.

  5. 5

    See what JSON keeps

    medium10 min

    Stringify an object with a null and an undefined property.

Find the bug(5)

  1. 1

    Fix the check that misses undefined

    easy8 min

    Strict equality against null does not catch undefined.

  2. 2

    Fix the zero rejected as missing

    easy8 min

    A truthy check treats a valid zero as absent.

  3. 3

    Fix typeof used to detect null

    easy8 min

    typeof null is object, so it cannot identify null.

  4. 4

    Fix the property set to undefined

    easy8 min

    Setting a property to undefined leaves it in the object but drops it from JSON.

  5. 5

    Fix the wrong emptiness assigned

    easy8 min

    A deliberate emptying should use null, not the text undefined.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace