Thursday, September 4, 2025
HomeLanguagesJavascriptJavaScript Error message Property

JavaScript Error message Property

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: 

Below is the example of the Error message property.

Example 1: 

JavaScript




try {
    allert("A computer science portal");
}
catch(err) {
    document.write(err.message);
}


Output:

allert is not defined

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

html




<!DOCTYPE html>
<html>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </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 3: This example displays an error message when use misspelled ‘alert’ statement. 

html




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

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS