JavaScript, inside out · Chapter 9 of 51

Null, undefined and NaN

By the end of this chapter you can tell the three kinds of "no proper value" apart, find NaN for certain, and fill in a default only where a value was never given. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First day at Tapri
  • 4 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Ravi has wired a thermometer into the milk boiler, and its readings are a mess. Sometimes the app has not asked it yet, sometimes it is unplugged, and sometimes it answers with garbage. The kitchen screen showed "undefined°C" for the first two and "NaN°C" for the third, which nobody could explain to Meera. Ravi also needs a quantity box that says clearly when there is no number, and orders where a missing quantity becomes 1 but a note the customer cleared stays cleared.

With what you know so far, you might test the reading as a value on its own in a condition. That lumps all three problems together, and it also throws away a real reading of 0°C, because 0 is falsy. You need a separate test for each kind of missing value.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    parseQty: a number, or null when there is none

    Write parseQty(text). When the text is a number, spaces around it allowed, return that number: parseQty('3') gives 3, parseQty(' 4 ') gives 4, and parseQty('0') gives 0, which is a real answer.

    When there is no number, return null: for blank text (empty, or only spaces) and for text that Number cannot read, like 'two' or '3 cups'.

  2. Task 2

    cleanReading: a label for every kind of reading

    Write cleanReading(reading). Return 'Not read yet' for undefined, 'No sensor' for null and 'Sensor error' for NaN. For any other number, return the number followed by °C: cleanReading(85) gives '85°C'.

    0 is a real reading: cleanReading(0) gives '0°C'.

  3. Task 3

    fillMissing: defaults that keep a cleared note

    Write fillMissing(order). Return a new object with three keys: item (copied from the order), qty and note. A qty that is undefined (missing, or set to undefined) becomes 1, and a note that is undefined becomes ''. Every other value is kept as it is, so a qty of 0 stays 0 and a note set to null stays null.

    fillMissing({ item: 'Masala chai' }) gives { item: 'Masala chai', qty: 1, note: '' }. The order you are given must not change.

  4. Task 4 · interview question

    Interview: emptinessPredictions, NaN, isNaN and Object.is

    Export emptinessPredictions, an array of ten values: what each expression in the comment above it gives, in the same order. Nine of them are true or false, and the typeof one is text, written as a string.

    The check works out the same ten expressions and compares.

What you will learn

  • undefined and null: never given, or empty on purpose.
  • NaN: where it comes from, and how to find it with Number.isNaN.
  • isNaN and Object.is: an older check and a stricter comparison, both interview favourites.
  • Defaults for undefined only: filling a gap without undoing a choice.

More in First day at Tapri

Build "Null, undefined and NaN" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.