Common Error Types

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Every Error Has a Family Name

Almost every error you will meet in JavaScript belongs to one of four families: `TypeError`, `ReferenceError`, `RangeError`, or `SyntaxError`. The most common of all is `TypeError`. It happens when you try to use a value in a way that its type does not support β€” like reading a property on `undefined`, or calling a number as if it were a function:

const level = 5; console.log("Level is " + level); level(); // error!

Because `level` is a number (`5`) and not a function, calling `level()` throws `TypeError: level is not a function`. Whenever you see `TypeError`, ask yourself: *"What kind of value did this line expect, and what value did it actually receive?"*

Exercise 2 β€” ReferenceError: The Name Doesn't Exist

The second family is `ReferenceError`. It happens when JavaScript searches every accessible scope for a variable name and finds nothing. The two everyday causes are a spelling typo or trying to use a variable outside the block where it was declared:

if (true) { const bonus = 50; } console.log("Bonus computed"); console.log(bonus); // error!

Be careful not to confuse `ReferenceError` and `TypeError`: 1. `ReferenceError` β€” The variable name itself does not exist in scope. 2. `TypeError` β€” The variable exists, but holds the wrong kind of value.

Exercise 3 β€” RangeError: A Number Out of Bounds

The third family is `RangeError`. Here, you passed a number (so its type is right!), but that number is outside the allowed range. A classic example is passing a negative number to `.repeat()`, which repeats a string a given number of times:

const hp = 30; const damage = 50; const left = hp - damage; console.log("HP left: " + left); console.log("#".repeat(left)); // error!

Because `30 - 50` is `-20`, calling `"#".repeat(-20)` throws `RangeError: Invalid count value: -20`. A `RangeError` almost always means a calculation went below zero (or above a maximum limit). The fix is to clamp the number before using it:

const hp = 30; const damage = 50; let left = hp - damage; if (left < 0) { left = 0; } console.log("[" + "#".repeat(left) + "]");

Exercise 4 β€” Quiz: Which Type?

Which error type does this code produce?

const cdm = { name: "Zap" }; cdm.attack();
Options
β€’ TypeError
β€’ ReferenceError
β€’ RangeError
β€’ SyntaxError

Hint : The variable exists, but what you're calling on it is not a function.

Exercise 5 β€” Quiz: Missing Name

Which error type does this code produce?

const trainer = "Sam"; console.log(trainer.length + trainerAge);
Options
β€’ ReferenceError
β€’ TypeError
β€’ RangeError
β€’ SyntaxError

Hint : One of the two names was never declared anywhere.

Exercise 6 β€” Quiz: Out of Bounds

Which error type does this code produce?

const width = -3; console.log("=".repeat(width));
Options
β€’ RangeError
β€’ TypeError
β€’ ReferenceError
β€’ SyntaxError

Hint : The value is a valid number, just not one this method accepts.

Exercise 7 β€” Summary: The 4 Error Families

Together with `SyntaxError` from Chapter 1, you now know all four main error families in JavaScript: 1. `TypeError` β€” Wrong kind of value (such as calling a method on `undefined`, or calling a number like a function). 2. `ReferenceError` β€” Unknown variable name (a spelling typo, or a variable used outside its scope). 3. `RangeError` β€” A number is outside the allowed bounds (such as `"#".repeat(-5)`). 4. `SyntaxError` β€” Broken code grammar (like a missing bracket or quote). Stops the program before the first line even runs.

Errors are objects too!

In JavaScript, every error is an object carrying its family name in `error.name` and its description in `error.message`. In the next chapters, you will learn how to catch and read these error objects directly in your code!

Exercise 8 β€” Clamp Before Drawing

Stop the bar from crashing when HP goes negative.

Hint : Catch the value when it drops too low, and bring it back to the smallest allowed count.

Exercise 9 β€” Error Type Cheat Sheet

Match each situation with the error type it produces.

Items to match
β€’ undefined.name
β€’ A variable that was never declared
β€’ "x".repeat(-1)
β€’ const a = ;
Definitions
β€’ TypeError
β€’ ReferenceError
β€’ RangeError
β€’ SyntaxError

Exercise 10 β€” Sort the Bug Reports

The team sent you a pile of error messages. Sort them by family and count each one.

Step 1: `typeOf(message)` returns the error type written before the colon. `"TypeError: x is not a function"` gives `"TypeError"`. Step 2: build a Map counting how many messages belong to each type. Step 3: print one line per type, in the format `"Type: N"`, in the order the types were first met. Step 4: print `"Most common: X"` with the type that appears most often.

Expected output:

TypeError: 3 ReferenceError: 1 RangeError: 1 Most common: TypeError
Starter code
const reports = [ "TypeError: cdm.attack is not a function", "ReferenceError: trainr is not defined", "TypeError: Cannot read properties of undefined", "RangeError: Invalid count value: -2", "TypeError: level is not a function" ]; // Write your code here
Back to the course