codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

41 / 65 topics
39JavaScript try...catch...finally40JavaScript throw Statement41JavaScript Error Objects42JavaScript "use strict"43Debugging JavaScript
Tutorials/JavaScript/JavaScript Error Objects
🌐JavaScript

JavaScript Error Objects

Updated 2026-05-12
20 min read

JavaScript Error Objects

In the previous section, we explored how to use the throw statement to create custom errors. However, JavaScript provides several built-in error types that help us handle common issues more effectively. Understanding these error objects and their properties is crucial for robust error handling in your applications.

Introduction

Error objects in JavaScript are instances of the Error class or its subclasses. They provide information about runtime errors, such as type mismatches or undefined variables. Knowing how to work with these built-in error types can help you write more reliable code and make debugging easier.

In this tutorial, we'll cover two common built-in error types: TypeError and ReferenceError. We'll also explore the properties of error objects, specifically name and message, which are essential for understanding and handling errors effectively.

Built-in Error Types

TypeError

A TypeError is thrown when an operation is applied to a value of the wrong type. This can happen in various scenarios, such as calling a method on a non-object or using an operator with incompatible types.

Example 1: Calling a Method on a Non-Object

JavaScript
1// script.js
2let num = 5;
3num.toUpperCase();
Output

While this code doesn't throw a TypeError, it demonstrates the importance of type checking. If we were to perform an operation that strictly requires numbers, such as division, a TypeError would occur.

ReferenceError

A ReferenceError is thrown when trying to access a variable or function that hasn't been declared or is out of scope.

Example 3: Accessing an Undefined Variable

JavaScript
1// script.js
2console.log(undeclaredVariable);
Output

Here, innerFunction is declared inside outerFunction, so it's not accessible outside of outerFunction.

Error Properties

Every error object in JavaScript has several properties that provide information about the error. The two most commonly used properties are name and message.

name

The name property specifies the type of error. For built-in errors, this is usually a string like "TypeError" or "ReferenceError". You can also set this property for custom errors.

Example 5: Accessing the name Property

JavaScript
1// script.js
2try {
3 let num = 5;
4 num.toUpperCase();
5} catch (error) {
6 console.log(error.name); // Output: TypeError
7}
Output

Practical Example

Let's create a simple program that demonstrates the use of TypeError and ReferenceError, as well as how to handle them using try-catch blocks.

JavaScript
1// script.js
2function divide(a, b) {
3 if (typeof a !== 'number' || typeof b !== 'number') {
4 throw new TypeError('Both arguments must be numbers');
5 }
6 if (b === 0) {
7 throw new Error('Division by zero is not allowed');
8 }
9 return a / b;
10}
11
12try {
13 console.log(divide(10, 2)); // Output: 5
14 console.log(divide('ten', 2)); // Throws TypeError
15} catch (error) {
16 console.log(`Error: ${error.name} - ${error.message}`);
17}
18
19try {
20 console.log(myUndefinedVariable); // Throws ReferenceError
21} catch (error) {
22 console.log(`Error: ${error.name} - ${error.message}`);
23}
Output
5
Error: TypeError - Both arguments must be numbers
Error: ReferenceError - myUndefinedVariable is not defined

In this example, we define a divide function that checks the types of its arguments and throws appropriate errors if necessary. We then use try-catch blocks to handle these errors gracefully.

Summary

  • Built-in Error Types: JavaScript provides several built-in error types like TypeError and ReferenceError, which help identify common runtime issues.
  • Error Properties: Every error object has properties like name and message that provide information about the error type and description, respectively.
  • Handling Errors: Using try-catch blocks allows you to handle errors gracefully and prevent your program from crashing.

What's Next?

Now that you understand how to work with built-in error types and their properties, let's explore how to use "use strict" in JavaScript. This feature helps catch common coding mistakes and enforces stricter parsing rules, making your code more robust and secure.

Stay tuned for the next tutorial on "JavaScript 'use strict'".


PreviousJavaScript throw StatementNext JavaScript "use strict"

Recommended Gear

JavaScript throw StatementJavaScript "use strict"