Skip to content

throw

In JavaScript, the throw keyword throws an exception, which stops the current code and passes the thrown value to the nearest enclosing catch clause up the call stack. If no catch clause handles it, the exception is uncaught, which ends a Node.js process and shows an error in a browser console. Any finally blocks along the way still run.

Any value can be thrown, but code usually throws an Error object or a subclass such as TypeError or RangeError, because those carry a .message, a .name, and a stack trace. Inside an async function, a throw that nothing catches rejects the promise the call returned.

The expression must start on the same line as throw, so a line break right after the keyword is a SyntaxError. throw is a statement, not an expression, so it can’t appear in places such as the right side of ??.

JavaScript throw Keyword Syntax

A throw statement takes a single expression:

Language: JavaScript Syntax
throw <expression>;

JavaScript throw Keyword Examples

The setAge() function throws a RangeError for invalid input:

Language: JavaScript
> function setAge(age) {
…   if (!Number.isInteger(age) || age < 0) {
…     throw new RangeError(`Invalid age: ${age}`);
…   }
…   return age;
… }
undefined

> setAge(30)
30

> try {
…   setAge(-5);
… } catch (error) {
…   console.log(`${error.name}: ${error.message}`);
… }
RangeError: Invalid age: -5
undefined

The invalid input stops setAge() before its return runs, and the surrounding try statement hands the RangeError to its catch block.

JavaScript throw Keyword Use Cases

  • Rejecting invalid arguments or input
  • Signaling states that a function can’t handle, such as a missing record
  • Rethrowing an error from a catch block after logging it
  • Wrapping a low-level error in a more descriptive one

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 5, 2026