Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML DOMException code property

  • Last Updated : 14 Jul, 2020

The code property of the DOMException interface returns a short number that contains the error code of the exception error, or 0 if none match. This is a read-only property.

Syntax:

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!

var codeNumber = domException.code;

Return Value: This property returns a short number.

Example: In this example, we will create an INVALID_CHARACTER_ERR and then show its code.






<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>DOMException code example</title>
</head>
<body>
  <h1>GeeksforGeeks</h1>
  <p>Click below to get the exception code</p>
  <button onclick="get()">Click</button>
  <p class="p">Error Code is : </p>
</body>
<script>
    function get(){
        try {
                // INVALID_CHARACTER_ERR
            var elem = document.createAttribute ("123");
        }
        catch (e) {
            console.log("Error is :", e);
            console.log("Errorcode is :", e.code);
            document.querySelector(
      ".p").textContent+=e.code;
        }   
        
</script>
</html>

Output:

Before Button Click:

After Button Click:

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!