Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

When Numbers Surprise You

In this video, you followed one bad weight through a pack script and saw how far its NaN travels.

Key Points

  • NaN spreads: Adding NaN to other numbers gives NaN, so one bad conversion turns the whole total into NaN, and the good weights get lost with it.
  • Comparisons with NaN answer false: Asking whether NaN is bigger, smaller or equal always gives false. A false like Light enough: false can look like a real answer when the script never knew the weight.
  • NaN isn’t equal to itself: Number("1.5kg") === NaN is false, because NaN isn’t equal to anything, not even another NaN. Check with Number.isNaN() instead, which gives true for NaN.
  • Trace a strange number back: When a printed number looks wrong, follow it up the script, line by line, to the first line that made it.
  • Decimals can be a tiny bit off: 0.1 + 0.2 gives 0.30000000000000004, because most decimals can’t be stored exactly. For now, round only when you show a number. For money, count in whole cents, because small whole-cent amounts add up exactly.
  • Dividing by zero doesn’t stop your program: 6 / 0 gives Infinity, a number bigger than every finite number.

The Script

The pack script from the video, with the sleeping bag’s unit still stuck on:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, you used Number() to turn numeric text into real numbers. This pack script does that for three weights from a gear list, then checks the total against an eight kilogram limit.

00:12 But one weight arrived with its unit stuck on. The sleeping bag’s text has its kilograms written right after the number, all in one string. In this video, you’ll follow what that one bad value does to the rest of the script.

00:27 JavaScript runs the script from the top, one line at a time. The tent’s text converts to the number 2.5. The sleeping bag’s text doesn’t spell a number, so Number() gives back NaN, the not-a-number value from the last lesson.

00:45 The food converts to two, as you’d expect. Now the next line adds all three weights. Your turn: pause the video and predict what totalKg holds after this line.

01:02 It holds NaN, the same value that came out of the sleeping bag line. Adding NaN to other numbers gives NaN, so the two good weights get lost in the total.

01:15 The value keeps travelling. The comparison asks whether NaN is at most eight. Asking whether NaN is bigger, smaller or equal always answers false, so false is what isLight stores.

01:30 Now run the script with node, and the terminal prints both lines. The total shows NaN kilograms.

01:38 The second line is the sneaky one. It says false, and that looks like a real answer, but the script never knew the weight. So when a strange number shows up, follow it back up the script.

01:51 The total came from the addition. The addition used the sleeping bag’s weight. And that weight came from the text with its unit. That’s the first bad line.

02:03 Fix the text to "1.5", run the script again, and the pack weighs six kilograms. This time, it’s light enough, too.

02:14 So how could your code notice a NaN before it spreads? Comparing the bag’s weight with NaN using triple equals seems natural, so try it in the Node REPL.

02:23 The answer is false, even though that weight really is NaN. That’s the comparison rule again. NaN isn’t equal to anything, not even another NaN.

02:34 That’s why the last lesson checked with Number.isNaN(), which answers true here.

02:41 Two more number results can surprise you. Add 0.1 and 0.2, and the REPL shows a number a tiny bit bigger than 0.3. JavaScript can’t store most decimals, like 0.1, exactly.

02:57 The leftover is tiny, so for now, round only when you show a number. You’ll learn how in Round the Way Your Story Needs. For money, count in whole cents instead, because small whole-cent amounts add up exactly.

03:13 The other surprise is dividing by zero. Share the pack’s six kilograms among zero hikers, and JavaScript doesn’t stop with an error. It gives Infinity, a number that’s bigger than every finite number, every number you could write down.

03:29 Keep one rule in mind. Adding NaN to a number gives NaN. And asking whether NaN is bigger, smaller or equal answers false, every time.

03:41 So when a printed number looks strange, whether it’s NaN, Infinity or a long decimal, trace it back up your script to the line that made it.

03:51 In the next exercise, Spot Text That Isn’t a Number, you’ll check messy text with Number.isNaN() and spot the values that aren’t numbers.

Become a Member to join the conversation.