Explain JavaScript Error Object.

The Error object is thrown by the JavaScript interpreter when a script error occurs. It can also be used to create custom exceptions. Understanding Error objects helps in debugging and error handling in JavaScript applications.

Error Object Properties

Property Description
name Sets or returns the name/type of the error
message Sets or returns the error message as a string

Example: Catching and Displaying Error Properties

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Error Object</title>
<style>
    body {
        font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    }
    .result {
        font-size: 18px;
        font-weight: 500;
        color: red;
        margin: 20px 0;
    }
    button {
        padding: 10px 20px;
        font-size: 16px;
        cursor: pointer;
    }
</style>
</head>
<body>
<h1>JavaScript Error Object Example</h1>
<button onclick="triggerError()">Trigger Error</button>
<div class="result" id="result"></div>

<script>
function triggerError() {
    try {
        // This will cause a ReferenceError
        undefinedVariable.someMethod();
    } catch (error) {
        document.getElementById("result").innerHTML = 
            "Error Type: " + error.name + "<br>" +
            "Error Message: " + error.message + "<br>" +
            "Full Error: " + error.toString();
    }
}
</script>
</body>
</html>

Creating Custom Error Objects

// Creating a custom error
function validateAge(age) {
    if (age < 18) {
        throw new Error("Age must be 18 or older");
    }
    return "Valid age: " + age;
}

try {
    console.log(validateAge(16));
} catch (error) {
    console.log("Error name:", error.name);
    console.log("Error message:", error.message);
}

try {
    console.log(validateAge(25));
} catch (error) {
    console.log("This won't execute");
}
Error name: Error
Error message: Age must be 18 or older
Valid age: 25

Common Error Types

// Different types of errors and their names
try {
    nonExistentVariable;
} catch (error) {
    console.log("ReferenceError:", error.name, "-", error.message);
}

try {
    null.someMethod();
} catch (error) {
    console.log("TypeError:", error.name, "-", error.message);
}

try {
    JSON.parse("{invalid json}");
} catch (error) {
    console.log("SyntaxError:", error.name, "-", error.message);
}
ReferenceError: ReferenceError - nonExistentVariable is not defined
TypeError: TypeError - Cannot read properties of null (reading 'someMethod')
SyntaxError: SyntaxError - Unexpected token i in JSON at position 1

Key Points

  • Every Error object has name and message properties
  • Use throw new Error() to create custom errors
  • The toString()
  • Different error types include ReferenceError, TypeError, SyntaxError, and more

Conclusion

Error objects provide essential information for debugging JavaScript applications. Use the name and message properties to identify and handle different error types effectively.

Updated on: 2026-03-15T23:18:59+05:30

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements