Node.nodeType

The read-only Node.nodeType property is an integer that identifies what the node is. It distinguishes different kind of nodes from each other, such as elements, text and comments.

Syntax

var type = node.nodeType;

Returns an integer which specifies the type of the node. Possible values are listed in Node type constants.

Constants

Node type constants

Constant Value Description
Node.ELEMENT_NODE 1 An Element node like <p> or <div>.
Node.ATTRIBUTE_NODE 2 An Attribute of an Element.
Node.TEXT_NODE 3 The actual Text inside an Element or Attr.
Node.CDATA_SECTION_NODE 4 A CDATASection, such as <!CDATA[[ … ]]>.
Node.PROCESSING_INSTRUCTION_NODE 7 A ProcessingInstruction of an XML document, such as <?xml-stylesheet … ?>.
Node.COMMENT_NODE 8 A Comment node, such as <!-- … -->.
Node.DOCUMENT_NODE 9 A Document node.
Node.DOCUMENT_TYPE_NODE 10 A DocumentType node, such as <!DOCTYPE html>.
Node.DOCUMENT_FRAGMENT_NODE 11 A DocumentFragment node.

Deprecated node type constants

The following constants have been deprecated and should not be used anymore.

Constant Value Description
Node.ENTITY_REFERENCE_NODE 5 An XML Entity Reference node, such as &foo;. Removed in DOM4.
Node.ENTITY_NODE 6 An XML <!ENTITY …> node. Removed in DOM4.
Node.NOTATION_NODE 12 An XML <!NOTATION …> node. Removed in DOM4.

Examples

Different types of nodes

document.nodeType === Node.DOCUMENT_NODE; // true
document.doctype.nodeType === Node.DOCUMENT_TYPE_NODE; // true

document.createDocumentFragment().nodeType === Node.DOCUMENT_FRAGMENT_NODE; // true

var p = document.createElement("p");
p.textContent = "Once upon a time…";

p.nodeType === Node.ELEMENT_NODE; // true
p.firstChild.nodeType === Node.TEXT_NODE; // true

Comments

This example checks if the first node inside the document element is a comment, and displays a message if not.

var node = document.documentElement.firstChild;
if (node.nodeType !== Node.COMMENT_NODE) {
  console.warn("You should comment your code!");
}

Specifications

Specification Status Comment
DOM
The definition of 'Node.nodeType' in that specification.
Living Standard Deprecated ATTRIBUTE_NODE, ENTITY_REFERENCE_NODE and NOTATION_NODE types.
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Node.nodeType' in that specification.
Obsolete No changes.
Document Object Model (DOM) Level 2 Core Specification
The definition of 'Node.nodeType' in that specification.
Obsolete No changes.
Document Object Model (DOM) Level 1 Specification
The definition of 'Node.nodeType' in that specification.
Obsolete Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
nodeTypeChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 6Opera Full support 7Safari Full support 1.1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support