HTML DOM nodeType Property

The HTML DOM nodeType property returns a number corresponding to the type of the node.

Following is the syntax −

Returning number value


Here, the return value can be the following −

  • Number ‘1’ for element nodes
  • Number ‘2’ for attribute nodes
  • Number ‘3’ for text nodes
  • Number ‘8’ for comment nodes

NOTE: Whitespaces are considered as text nodes only.

Let us see an example of HTML DOM nodeType property −


 Live Demo

<!DOCTYPE html>
<title>HTML DOM nodeType</title>
   form {
      margin: 0 auto;
      text-align: center;
   * {
      padding: 2px;
   input[type="button"] {
      border-radius: 10px;
         <label for="example">Example site: </label>
         <input id="urlSelect" type="url" name="urlAddress" value="" disabled>
         <input type="button" onclick="getNodeTypes()" value="Get all Types">
         <div id="divDisplay"></div>
   var urlSelect = document.getElementById("urlSelect");
   var commentNode = document.createComment("My Example URL");
   var divDisplay = document.getElementById("divDisplay");
   function getNodeTypes() {
      divDisplay.textContent = 'Body: '+document.body.nodeType;
      divDisplay.textContent += ' & input: '+urlSelect.nodeType;
      divDisplay.textContent += ' & comment: '+commentNode.nodeType;


Before clicking ‘Get all Types’ button −

After clicking ‘Get all Types’ button −