← Back to Chapters

JavaScript Errors

? JavaScript Errors

⚡ Quick Overview

JavaScript errors occur when the JavaScript engine cannot execute your code correctly. This may be due to syntax mistakes, invalid operations, or referencing variables that do not exist. When something goes wrong, JavaScript throws an error object and the current execution stops unless the error is handled.

Understanding error types and how to handle them using try...catch and finally is essential for building reliable applications.

? Key Concepts

  • Error – An object that represents something that went wrong during code execution.
  • Throwing an error – The act of signaling that an error has occurred (using the engine or throw keyword).
  • Catching an error – Using try...catch to handle errors gracefully.
  • Built-in error types – Standard error classes provided by JavaScript.

Common built-in error types in JavaScript include:

  • SyntaxError: Invalid JavaScript syntax, usually caught before the code runs.
  • ReferenceError: Using a variable or function that has not been declared.
  • TypeError: Performing an operation on a value of the wrong type.
  • RangeError: Using a numeric value that is outside the allowed range.
  • EvalError: Related to incorrect usage of the eval() function.
  • URIError: Passing malformed values to URI-handling functions.

? Syntax and Theory

The basic syntax for handling errors in JavaScript is the try...catch structure:

try contains code that might fail. If an error occurs inside try, the control jumps to the catch block, where you can access the error object. An optional finally block always runs, whether an error occurred or not.

? Basic try...catch...finally Structure
try {
// Code that may throw an error
} catch (error) {
// Handle the error
} finally {
// Cleanup code (always runs)
}

JavaScript uses the Error object to represent problems. An Error object typically contains:

  • name: Type of error (example: ReferenceError).
  • message: A human-readable explanation.

? Code Examples

? ReferenceError Example

? View Code Example
// Variable not declared
console.log(x);

?️ Handling Errors with try...catch

? View Code Example
try {
// y is not defined
let result = y + 10;
} catch (error) {
console.log("Error caught: " + error.message);
}

? Using the finally Block

? View Code Example
try {
let result = 10 / 2;
console.log("Result:", result);
} catch (e) {
console.log("Error:", e.message);
} finally {
console.log("Cleanup actions here");
}

⚠️ Throwing Custom Errors

? View Code Example
let age = -5;

if (age < 0) {
throw new Error("Age cannot be negative");
}

? Working with the Error Object

? View Code Example
try {
throw new Error("Something went wrong");
} catch (err) {
console.log(err.name);
console.log(err.message);
}

? Live Output / Explanation

? What the Console Shows

  • console.log(x)ReferenceError: x is not defined
  • y + 10 inside tryError caught: y is not defined
  • finally block runs even if an error occurs
  • Throwing age error → Uncaught Error: Age cannot be negative

? Tips & Best Practices

  • Wrap risky operations in try...catch.
  • Always throw Error objects instead of plain strings.
  • Use finally for cleanup logic.
  • Make messages descriptive and human-readable.
  • Read console errors carefully.

? Try It Yourself

  • Trigger a TypeError using null.toUpperCase().
  • Wrap JSON.parse() inside try...catch.
  • Create your own validation function with throw new Error().
  • Test ReferenceError, TypeError, and RangeError.
  • Always add a finally block.