JavaScript Error name Property

The name property of JavaScript Error objects identifies the type of error that occurred. It returns a string representing the error's name, which helps in debugging and error handling.

Syntax

error.name

Common Error Names

Different error types have specific names:

  • ReferenceError - Variable or function not defined
  • TypeError - Wrong data type used
  • SyntaxError - Invalid syntax
  • RangeError - Number out of range

Example: Displaying Error Names

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Error Name Property</title>
    <style>
        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
        }
        .result {
            font-size: 18px;
            margin: 10px 0;
            padding: 10px;
            background-color: #f8f9fa;
            border-left: 4px solid #007bff;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>JavaScript Error name Property</h1>
    
    <button onclick="testReferenceError()">Test ReferenceError</button>
    <button onclick="testTypeError()">Test TypeError</button>
    <button onclick="testRangeError()">Test RangeError</button>
    
    <div id="output" class="result">Click buttons to see error names</div>
    
    <script>
        function testReferenceError() {
            try {
                undefinedVariable.toString();
            } catch (error) {
                document.getElementById('output').innerHTML = 
                    `Error Name: ${error.name}<br>Message: ${error.message}`;
            }
        }
        
        function testTypeError() {
            try {
                null.toString();
            } catch (error) {
                document.getElementById('output').innerHTML = 
                    `Error Name: ${error.name}<br>Message: ${error.message}`;
            }
        }
        
        function testRangeError() {
            try {
                let arr = new Array(-1);
            } catch (error) {
                document.getElementById('output').innerHTML = 
                    `Error Name: ${error.name}<br>Message: ${error.message}`;
            }
        }
    </script>
</body>
</html>

Setting Custom Error Names

You can create custom errors and set their names:

try {
    let customError = new Error("Something went wrong");
    customError.name = "CustomError";
    throw customError;
} catch (error) {
    console.log("Error Name:", error.name);
    console.log("Error Message:", error.message);
}
Error Name: CustomError
Error Message: Something went wrong

Practical Use Case

Use error names for specific error handling:

function processData(data) {
    try {
        if (data === null) {
            throw new TypeError("Data cannot be null");
        }
        if (data.length === 0) {
            throw new RangeError("Data array is empty");
        }
        console.log("Processing:", data);
    } catch (error) {
        switch (error.name) {
            case "TypeError":
                console.log("Type Error:", error.message);
                break;
            case "RangeError":
                console.log("Range Error:", error.message);
                break;
            default:
                console.log("Unknown Error:", error.name);
        }
    }
}

processData(null);
processData([]);
Type Error: Data cannot be null
Range Error: Data array is empty

Conclusion

The name property provides essential error identification for debugging and conditional error handling. Use it to create more robust error management in your applications.

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

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements