JavaScript | Error name Property

It is used to set or return the name of an error.

Syntax:

errorObj.name

Parameter Values:

  • 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.

Example-1: This example shows Syntax Error.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <h1>
      <center>
        Geeks
      </center
  </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>

chevron_right


Output:

Example-2: This example shows Range Error.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <h1>
      <center>
        Geeks
      </center
  </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>

chevron_right


Output:

Example-3: This example shows Reference Error.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <h1>
      <center>
        Geeks
      </center>
  </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>

chevron_right


Output:

Example-4: This example shows Type Error.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <h1>
      <center>
        Geeks
      </center>
  </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>

chevron_right


Output:

Browser Support: The listed browsers support error name property.

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai