Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 13.1 — Error Types
JavaScript · Example 1 of 3

Every common error type, caught and printed

Printing an error shows name: message. The exact wording is Chrome's; Firefox and Safari use different messages for the same names.

When JavaScript can't do what you asked, it creates an error object with a name, a message and a stack trace; the name (SyntaxError, ReferenceError, TypeError, RangeError and a few more) tells you what kind of mistake it is, and the stack trace tells you where.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write code that triggers a RangeError from "ab".repeat(-1), catches it, and prints only its name.

02

This function crashes for one of the three users. Without running it, say which error and for which user, then fix it so it prints No email for that user: const users = [{ email: "a@x.in" }, { email: "b@x.in" }, {}]; users.forEach((u) => console.log(u.email.toLowerCase()));

Explain it without notes

01

What are the main built-in error types, and what does each one tell you?

02

What is the difference between a SyntaxError in your script and one from JSON.parse?

03

How do you read a stack trace, and why isn't the top line always the bug?

04

Why shouldn't your code depend on the text of an error message from the engine?

Every common error type, caught and printed

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

ReferenceError: totl is not defined
TypeError: Cannot read properties of null (reading 'length')
TypeError: user.greet is not a function
TypeError: Assignment to constant variable.
RangeError: Invalid array length
RangeError: toFixed() digits argument must be between 0 and 100
SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)
URIError: URI malformed