Skip to content

Errors

Learn what errors are, the difference between syntax and runtime errors, and how to handle them gracefully.

What you'll learn

  • What an error is and why programs produce them
  • The difference between syntax, runtime, and logic errors
  • How try/catch lets you handle errors gracefully
  • How to read an error message and stack trace

Concept

Errors

An error is a signal that something went wrong while running your program. Errors are not failures of you as a programmer — they are normal, and learning to read and handle them is a core skill.

Three Kinds of Errors

  1. Syntax errors — the code is malformed and cannot be parsed at all. A missing bracket or stray comma. The program never runs.
  2. Runtime errors — the code is syntactically valid but fails during execution. Calling a function that does not exist, or reading a property of null.
  3. Logic errors — the program runs without crashing but produces the wrong result. The hardest to find, because no error message is shown.

Reading Error Messages

A runtime error prints a message and a stack trace — the chain of function calls that led to the error. Read the trace from the top: the first line usually names the file and line number where the error occurred.

TypeError: Cannot read properties of undefined (reading 'name')
    at getUser (app.js:12)
    at render (app.js:25)

This tells you: at line 12 of app.js, inside getUser, the code tried to read .name from undefined.

try / catch

You can catch a runtime error and handle it gracefully instead of crashing:

try {
  const data = JSON.parse(brokenInput);
} catch (error) {
  console.log("Invalid JSON:", error.message);
}

The code in try runs normally; if it throws, execution jumps to catch with the error object. The program continues instead of crashing.

The Mindset

Errors are feedback, not punishment. Each error message is a clue. When you see one, read it carefully — it usually tells you exactly what went wrong and where.

Example

              // A function that can fail:
function parseAge(input) {
  const age = Number(input);
  if (Number.isNaN(age)) {
    throw new Error(`${input} is not a valid number`);
  }
  if (age < 0) {
    throw new Error("Age cannot be negative");
  }
  return age;
}

// Handle errors gracefully with try/catch:
try {
  const result = parseAge("twenty");
  console.log("Age:", result);
} catch (error) {
  console.log("Could not parse age:", error.message);
  // "Could not parse age: twenty is not a valid number"
}
            

parseAge throws custom errors for invalid input. The try/catch around the call prevents the program from crashing and shows a friendly message instead.

Try it

  • JSON Validator

    Paste broken JSON and read the error message. This tool catches syntax errors and shows you exactly where they are.

Common mistakes

The mistake

Ignoring error messages instead of reading them.

The fix

Read the message and stack trace. They usually name the file, line, and problem. A 30-second read can save 30 minutes of guessing.

The mistake

Using try/catch to silently swallow errors with an empty catch block.

The fix

At minimum, log the error in the catch block. Silent catches hide bugs and make debugging far harder later.

Related Tools

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors