Read the Clues in Wrong Output

Your packing list now shows a progress line per category, such as Clothes: 3 of 5 packed, counted from the items every time the page redraws. Counts like these are where wrong numbers creep in without a sound. The Console stays empty, the page looks finished, and the number is wrong.

In this lesson, you’ll learn to read what a wrong value hints at, and you’ll trace one back to the line where it first went wrong, so you fix the cause instead of hiding it. Start with what the wrong output itself can tell you.

Read the Clue in a Wrong Value

A silent bug is a flaw in code that runs to the end without an error but gives the wrong result. An error message names a line. A wrong value names nothing, so you have to work out where to look from the value itself.

Before you can call a value wrong, you need to know what the right one is. So work out the expected value by hand, from the data, before you run anything. Say Beach Week has two Clothes items and only the swimsuit is ticked. The expected line is Clothes: 1 of 2 packed. Whatever the page shows instead is the actual value, and the gap between the two is your first clue.

Some wrong values come up again and again, and each one points somewhere:

  • NaN: A calculation had no numeric answer. Often a value wasn’t a number, such as undefined, but 0 / 0 gives NaN too. Check the line that does the math, then the values that reached it.
  • undefined: A value you needed is missing, or an expression gave undefined. Check for a misspelled property, a missing key, or a function that forgot to return.
  • Glued digits, such as 53: A + met a string, so it joined text instead of adding. Check where the value was read in, such as a form field or a data-* attribute.
  • A count stuck at 0: The code that should change it may never have run, or something reset or replaced the count before it was shown. Check that the loop, the if, or the listener ran, then check what happens to the count afterward.
  • A loop that ran once: Something stopped it early, such as a return inside the loop. Check the loop’s body.

You can explore some of these clues in any Console. Open DevTools on a blank tab and try these:

Language: JavaScript
> undefined + 1
NaN
> "5" + 3
'53'
> "5" * 3
15

Adding 1 to undefined gives NaN, with no error. A string and a number joined with + give the string "53". With *, which only works on numbers, JavaScript converts "5" first and gets 15. That’s why glued digits nearly always mean +.

To see a suspect value, log it with a label so you can tell the lines apart. However, a string of digits looks a lot like a number. Here’s a count that holds a string, logged the usual way and then with typeof:

Language: JavaScript
const packed = "5" + 3;
console.log("packed:", packed);
console.log("packed:", packed, typeof packed);

In Chrome’s Console, a logged number shows in a different color from text, but the difference is small, so the first line can pass for a number. The second one spells out that packed is a string:

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

Become a Member to join the conversation.