Reading Error Messages
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β The Message Is a Clue
Beginners often panic when an error message pops up and start changing random lines of code. Experienced developers do the opposite: they read the message carefully. An error message tells you what went wrong and which word in your code caused the crash:
const team = ["Zap", "Aquachu"];
console.log("Checking the team...");
console.log(team[3].toUpperCase()); // error!Run the code above and look at the red message: `TypeError: Cannot read properties of undefined (reading 'toUpperCase')` Every JavaScript error message is split into two parts separated by a colon (`:`): 1. The error type (`TypeError`) β The general family of problem. 2. The description (`Cannot read properties of undefined (reading 'toUpperCase')`) β Exactly what failed.
Exercise 2 β Reading an Error Backwards
Here is the best trick for decoding `Cannot read properties of undefined (reading 'toUpperCase')`: read the clue from right to left! 1. `(reading 'toUpperCase')` β Find `.toUpperCase` in your code. 2. `of undefined` β Whatever is written immediately before `.toUpperCase()` evaluated to `undefined`! In `team[3].toUpperCase()`, the expression right before the dot is `team[3]`. Let's print `team[3]` on its own to see why:
const team = ["Zap", "Aquachu"];
console.log(team[3]);
console.log(team.length);Since `team` only has `2` elements (indices `0` and `1`), `team[3]` is `undefined` β and you cannot call `.toUpperCase()` on `undefined`!
Exercise 3 β Errors That Name a Variable
Another error message you will see all the time is `ReferenceError: ... is not defined`. This message is even more direct: the word right before `is not defined` is the exact variable name that JavaScript could not find:
const trainer = "Sam";
console.log(trainr); // error!
console.log(trainer);Run the code above. The error says `ReferenceError: trainr is not defined`. JavaScript is handing you the misspelled name on a silver platter! Nine times out of ten, `is not defined` means either a spelling typo (`trainr` instead of `trainer`) or a variable used outside its scope.
Exercise 4 β Quiz: What Is undefined?
The message says `Cannot read properties of undefined (reading 'level')`. Which expression is `undefined`?
const team = [{ level: 5 }];
console.log(team[1].level);team[1]levelteamconsole.logHint : Look at what sits right before the property named in the message.
Exercise 5 β Quiz: Which Family?
You see `hpp is not defined`. What is the most likely cause?
Hint : The message names something JavaScript looked for and never found.
Exercise 6 β Confirm the Suspect
Before fixing, print the piece the message points at, to see what it really holds.
Hint : Isolate the expression that sits before the dot in the failing line.
Exercise 7 β A Method to Follow Every Time
Whenever red text appears in the console, follow this 5-step checklist instead of guessing: 1. Read the type (`TypeError`, `ReferenceError`...) to know the family of the problem. 2. Find the word or property mentioned in the description. 3. Locate that exact word in your code. 4. Use `console.log()` to print the expression right before it on its own. 5. Check why that value isn't what you expected!
Exercise 8 β Message Decoder
Match each message fragment with what it means.
x is not definedCannot read properties of undefined(reading 'level')Exercise 9 β Error Detective
Three error messages came back from three battle scripts. Decode each one and print what a developer should inspect first.
Step 1: `decode(message)` returns the name a developer should look at. If the message ends with `"is not defined"`, return the word before it. If it contains `"(reading '"`, return the property name between the quotes. Otherwise return `"unknown"`. Step 2: for each message in `messages`, print `"Inspect: X"` with the decoded name.
Expected output:
Inspect: trainr
Inspect: level
Inspect: unknownconst messages = [
"trainr is not defined",
"Cannot read properties of undefined (reading 'level')",
"Maximum call stack size exceeded"
];
// Write your function here
// Use it here