Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Error message Property

  • Last Updated : 03 Jul, 2020

Below is the example of the Error message property.

  • Example:




    <script>
        try {
             allert("A computer science portal");
            }
         catch(err) {
             document.write(err.message);
            }
    </script>
  • Output:
    allert is not defined

In JavaScript error message property is used to set or return the error message.

Syntax:

errorObj.message

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

More example codes for the above property are as follows:

Example 1: This example does not contains any error so it does not display error message.




<!DOCTYPE html>
<html>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
  
        <h3>
            JavaScript Error Message Property
        </h3>
  
        <p id="gfg"></p>
  
        <script>
            try {
                alert("A computer science portal");
            }
            catch(err) {
                document.getElementById("gfg").innerHTML
                        = err.message;
            }
    </script>
    </body>
</html>                    

Output:

Example 2: This example displays an error message when use misspelled ‘alert’ statement.




<!DOCTYPE html>
<html>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
  
        <h3>
            JavaScript Error Message Property
        </h3>
  
        <p id="gfg"></p>
  
        <script>
            try {
                popup("A computer science portal");
            }
            catch(err) {
                document.getElementById("gfg").innerHTML
                        = err.message;
            }
        </script>
    </body>
</html>                         

Output:

Supported Browsers: The browser supported by JavaScript Error Message Property are listed below:

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!