Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Error name Property

JavaScript Error name Property

In JavaScript, the Error name property is used to set or return the name of an error. 

Syntax:

errorObj.name

Property values: This property contains six different values as described below:

  • SyntaxError: It represents a syntax error.
  • RangeError: It represents an error in the range.
  • ReferenceError: It represents an illegal reference.
  • TypeError: It represents a type error.
  • EvalError: It represents an error in the eval() function.
  • URIError: It represents an error in the encodeURI().

Return Value: It returns a string, representing the name of the error.

Below are examples of the Error name Property.

Example: 

JavaScript




try {
    eval("alert('neveropen)");
} catch (err) {
    document.write( err.name);
}


Output:

SyntaxError 

Example 2: This example shows Syntax Error. 

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        JavaScript Error name Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
 
    <h3>
        JavaScript Error Name Property
    </h3>
 
    <p id="gfg"></p>
 
    <script>
        try {
            eval("alert('Geeks for Geeks)");
        } catch (err) {
            document.getElementById(
                "gfg").innerHTML = err.name;
        }
    </script>
</body>
</html>


Output: 

Example 3: This example shows Range Error. 

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        JavaScript Error name Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
 
    <h3>
        JavaScript Error Name Property
    </h3>
 
    <p id="gfg"></p>
 
    <script>
        let num = 0;
        try {
            num.toPrecision(1000);
        }
        catch (err) {
            document.getElementById(
                "gfg").innerHTML = err.name;
        }
    </script>
</body>
</html>


Output: 

 

Example 4: This example shows Reference Error. 

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        JavaScript Error name Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
 
    <h3>
        JavaScript Error Name Property
    </h3>
 
    <p id="gfg"></p>
 
    <script>
        let y;
        try {
            y = x + y;
        }
        catch (err) {
            document.getElementById(
                "gfg").innerHTML = err.name;
        }
    </script>
</body>
</html>


Output: 

Example 5: This example shows Type Error. 

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        JavaScript Error name Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
 
    <h3>
        JavaScript Error Name Property
    </h3>
 
    <p id="gfg"></p>
 
    <script>
        let x = 1;
        try {
            x.toLowerCase();
        } catch (err) {
            document.getElementById(
                "gfg").innerHTML = err.name;
        }
    </script>
</body>
</html>


Output: 

Browser Support: The browsers supported by JavaScript Error name Property are listed below:

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments