Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Error name Property

  • Last Updated : 03 Jul, 2020

Below is the example of the Error name Property.

  • Example:




    <script>
        try {
                eval("alert('GeeksforGeeks)");
            
        catch (err) {
                document.write( err.name);
            }
    </script>
  • Output:
    SyntaxError

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

Syntax:

errorObj.name

Property values: This property conayains 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.



More example codes for the above property are as follows:

Program 1: This example shows Syntax Error.




<!DOCTYPE html>
<html>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </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:

Program 2: This example shows Range Error.




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

Output:

Program 3: This example shows Reference Error.




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

Output:

Program 4: This example shows Type Error.




<!DOCTYPE html>
<html>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
  
        <h3>
            JavaScript Error Name Property
        </h3>
  
        <p id="gfg"></p>
  
        <script>
            var 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :