Back to JavaScript Notes
Topic #363

Error Statements

You will be able to deliberately throw JavaScript Error objects to stop invalid execution and report clear failure reasons.

What it is

An error statement in JavaScript is a deliberate signal that something has gone wrong. The most common form is throw new Error("bad input"). It creates an Error object and raises it. JavaScript can throw any value, but Error objects are preferred because they carry useful debugging information. If no try...catch handles it, the current synchronous execution stops and the runtime reports an uncaught exception.

Mental model: throwing is like pulling an emergency brake. It does not return a value; it transfers control to the nearest matching catch block, or to the runtime if none exists.

Related terms: try, catch, finally, TypeError, RangeError, custom error, stack trace.

Why it matters

  • Prevents invalid data from spreading through functions.
  • Makes failures explicit instead of producing silent wrong results.
  • Provides a message and stack trace for debugging.
  • Works well with centralized error handling in catch blocks.
  • Helps API boundaries reject bad arguments early.

Syntax or steps

Smallest useful pattern:

if (!condition) {
  throw new Error("clear message");
}
  1. Check for an invalid state or argument.
  2. Create an Error object with a specific message.
  3. Use throw to stop normal execution.
  4. Handle it with try...catch if the caller can recover.

Example

function withdraw(balance, amount) {
  if (typeof amount !== "number" || Number.isNaN(amount)) {
    throw new TypeError("amount must be a number");
  }

  if (amount <= 0) {
    throw new RangeError("amount must be positive");
  }

  if (amount > balance) {
    throw new Error("insufficient funds");
  }

  return balance - amount;
}

try {
  console.log(withdraw(100, 30));
  console.log(withdraw(100, 150));
} catch (error) {
  console.log(error.name + ": " + error.message);
}

Part by part:

  • withdraw validates inputs before doing work.
  • TypeError and RangeError are built-in error types that communicate the kind of problem.
  • throw stops the function immediately.
  • The catch block receives the thrown error and prints its name and message.

Expected output:

70
Error: insufficient funds

Common mistakes

  • Throwing strings like throw "bad input". Prefer throw new Error("bad input") so you get a stack trace and standard properties.
  • Using vague messages like "error". Include what failed and why, such as "email must contain @".
  • Throwing inside loops without a reason. This can stop execution too early; validate once or collect errors if needed.
  • Catching and ignoring errors with an empty catch. At least log or rethrow if the caller cannot recover.

When to use it

Use throw when continuing would be unsafe or meaningless. Use return values when failure is expected and the caller can handle it as normal data.

SituationUseWhy
Invalid argument that cannot be processedthrow new Error(...)Stops bad state early.
Expected empty result, such as no search matchReturn null or empty arrayNot exceptional; caller can continue.
Recoverable network failureThrow or return result object depending on API designChoose one clear contract.
Debugging onlyconsole.errorDoes not stop execution.

Practice

Guided exercise: Write a function parseAge(value) that throws a TypeError if value is not a number, throws a RangeError if the number is negative, and returns the number otherwise.

function parseAge(value) {
  if (typeof value !== "number") {
    throw new TypeError("age must be a number");
  }
  if (value < 0) {
    throw new RangeError("age cannot be negative");
  }
  return value;
}

Challenge: Call parseAge("25") inside a try...catch and print the error message. Expected output: age must be a number.

Quick check

Question: What happens when you use throw new Error("bad input") inside a function with no surrounding try...catch?

Answer: The function stops, the error propagates upward, and if nothing catches it, the JavaScript runtime reports an uncaught exception.

Summary

Throwing deliberate errors turns silent failures into clear, debuggable stops. Use throw new Error(...) for invalid states, and handle expected failures with return values or try...catch depending on your program's contract.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Error Statements – FAQs

Quick answers about learning Error Statements in JavaScript.

This free note from Coding Now Tech Institute explains Error Statements in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Error Statements, is 100% free with no signup required.
With focused practice, most students grasp Error Statements in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now