← Back to JavaScript Course | Chapter 8: Error Handling | Lesson 4 of 6

JS Error Types

JavaScript has different kinds of errors, like TypeError and ReferenceError, each telling you a different kind of problem, like different warning lights on a dashboard. Knowing them helps you fix things faster.
Syntax
javascript
throw new Error("message");
throw new TypeError("message");
throw new RangeError("message");

Error

Error is the base type all built-in JavaScript errors inherit from, and is also what you extend when defining your own custom error classes, since every specific error type ultimately shares its core behavior.

उदाहरण: Error

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Throw a new `Error` with message "Generic problem"
  // Throw a new `Error` with message "Generic problem"
  throw new Error("Generic problem");
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof Error, e.message` to the console
  // Print `e instanceof Error, e.message` to the console
  console.log(e instanceof Error, e.message);
}

TypeError

TypeError is thrown when a value isn't of the type an operation expects — for example, calling a method on undefined or trying to invoke something that isn't a function.

उदाहरण: TypeError

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  null.name;
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof TypeError, e.message` to the console
  // Print `e instanceof TypeError, e.message` to the console
  console.log(e instanceof TypeError, e.message);
}

ReferenceError

ReferenceError is thrown when code refers to a variable that doesn't exist in any accessible scope, such as using an identifier before it's declared or after a typo.

उदाहरण: ReferenceError

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Print `undeclaredVar` to the console
  // Print `undeclaredVar` to the console
  console.log(undeclaredVar);
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof ReferenceError, e.message` to the console
  // Print `e instanceof ReferenceError, e.message` to the console
  console.log(e instanceof ReferenceError, e.message);
}

SyntaxError

SyntaxError is thrown when code can't even be parsed — invalid syntax, mismatched brackets, or malformed JSON passed to JSON.parse() are common triggers, and unlike other errors it usually can't be caught if it happens at load time.

उदाहरण: SyntaxError

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Call `JSON.parse("{bad json}")`
  // Call `JSON.parse("{bad json}")`
  JSON.parse("{bad json}");
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof SyntaxError, e.message` to the console
  // Print `e instanceof SyntaxError, e.message` to the console
  console.log(e instanceof SyntaxError, e.message);
}

Other Built-in Error Types

Other built-ins include RangeError (a value outside its allowed range, like an invalid array length) and URIError (malformed input to encodeURI/decodeURI functions).

उदाहरण: Other Built-in Error Types

javascript
try {
  new Array(-1); // invalid array length
} catch (e) {
  console.log(e instanceof RangeError, e.message);
}
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.