JavaScript Node nodeType Property

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is nodeType?

node.nodeType returns an integer identifying the kind of node (MDN) — element, text, comment, document, and more. Prefer named constants like Node.ELEMENT_NODE over magic numbers.

Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Note: unused legacy constants ENTITY_REFERENCE_NODE, ENTITY_NODE, and NOTATION_NODE are deprecated — the nodeType property itself is not.

Remember
node.nodeType  → integer kind

Common (HTML day-to-day):
  Node.ELEMENT_NODE            = 1
  Node.TEXT_NODE               = 3
  Node.COMMENT_NODE            = 8
  Node.DOCUMENT_NODE           = 9
  Node.DOCUMENT_FRAGMENT_NODE  = 11

Prefer: node.nodeType === Node.ELEMENT_NODE
Avoid:  node.nodeType === 1   (works, but less clear)

vs nodeName: number kind vs string label
GoalPrefer
Kind checknode.nodeType === Node.ELEMENT_NODE
String labelnode.nodeName
Element-only childrenparent.children (or filter by type)

How to Use nodeType

Read the integer, compare to a Node.*_NODE constant, then branch.

  1. Get a DOM node.
  2. Read node.nodeType.
  3. Compare with Node.ELEMENT_NODE, Node.TEXT_NODE, etc.
  4. Filter or branch based on the kind.
PieceRequired?Role
nodeYesAny DOM Node
Return—Integer kind code
ConstantsRecommendedNode.ELEMENT_NODE, Node.TEXT_NODE, …

Pseudocode

Pseudocode
t = node.nodeType

if t === Node.ELEMENT_NODE:      // 1
  // treat as Element
else if t === Node.TEXT_NODE:    // 3
  // text / whitespace
else if t === Node.COMMENT_NODE: // 8
  // HTML comment
else if t === Node.DOCUMENT_NODE:
  // the document

// filter childNodes to elements only:
for child in parent.childNodes:
  if child.nodeType === Node.ELEMENT_NODE:
    keep(child)

Cheat Sheet

Common constants you will see in HTML and JS DOM work.

ConstantValueMeans
Node.ELEMENT_NODE1Element (<p>, <div>, …)
Node.TEXT_NODE3Text / whitespace
Node.COMMENT_NODE8HTML comment
Node.DOCUMENT_NODE9The document
Node.DOCUMENT_TYPE_NODE10<!DOCTYPE html>
Node.DOCUMENT_FRAGMENT_NODE11DocumentFragment
Also2, 4, 7Attribute, CDATA, processing instruction

Live Preview

Pick a node and see its nodeType plus matching constant.

Demo nodes
Hello

Compact markup: #nt-box holds text then a comment as children.

Result
Choose a target, then Run…

JavaScript Examples

Short, copy-ready snippets based on MDN Node.nodeType. Use Try It Yourself for a live editor.

Example 1 — Element Is ELEMENT_NODE (1)

Created elements report type 1 (MDN-style).

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

console.log(p.nodeType);                       // 1
console.log(p.nodeType === Node.ELEMENT_NODE); // true
Try It Yourself

How It Works

  1. Create an element. createElement("p") builds a node.
  2. Read nodeType. Elements always report 1.
  3. Compare to the constant. Node.ELEMENT_NODE equals 1.
  4. Prefer the constant so readers see intent, not a magic number.

Example 2 — Text Content Is TEXT_NODE (3)

The text inside an element is its own node.

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

console.log(p.firstChild.nodeType);                    // 3
console.log(p.firstChild.nodeType === Node.TEXT_NODE); // true
Try It Yourself

How It Works

  1. Set textContent. The browser creates a Text child.
  2. Read firstChild.nodeType. That child is not an element.
  3. Match TEXT_NODE. Constant value is 3.
  4. Same for whitespace between pretty-printed tags.

Example 3 — Comment Check (MDN-style)

Warn when the first child is not a comment.

JavaScript
const node = document.getElementById("sample").firstChild;

if (node.nodeType !== Node.COMMENT_NODE) {
  console.warn("You should comment your code!");
} else {
  console.log("Found a comment node");
}
Try It Yourself

How It Works

  1. Read firstChild. In pretty HTML this is often #text.
  2. Compare to COMMENT_NODE. Value 8.
  3. Whitespace fails the check. So the warn path runs.
  4. Lesson: always classify with nodeType before assuming kind.

Example 4 — Document & Fragment Types

MDN-style checks for document, doctype, and fragments.

JavaScript
console.log(document.nodeType === Node.DOCUMENT_NODE); // true

const frag = document.createDocumentFragment();
console.log(frag.nodeType === Node.DOCUMENT_FRAGMENT_NODE); // true

if (document.doctype) {
  console.log(document.doctype.nodeType === Node.DOCUMENT_TYPE_NODE); // true
}
Try It Yourself

How It Works

  1. document is DOCUMENT_NODE (9).
  2. Fragments are type 11. Useful when batching inserts.
  3. doctype is type 10 when the page has a doctype.
  4. Same API everywhere. One integer property for every node kind.

Example 5 — Keep Only Element Children

Skip whitespace text nodes while walking childNodes.

JavaScript
const parent = document.getElementById("list");
const names = [];

for (const child of parent.childNodes) {
  if (child.nodeType === Node.ELEMENT_NODE) {
    names.push(child.nodeName);
  }
}

console.log(names.join(", ")); // "LI, LI, LI"
Try It Yourself

How It Works

  1. childNodes includes text. Pretty lists insert whitespace nodes.
  2. Filter by ELEMENT_NODE. Keep only real <li> tags.
  3. Collect nodeName. HTML reports "LI".
  4. Shortcut: parent.children is already element-only.

Edge Cases & Pitfalls

Check these before relying on nodeType in walks and filters.

constants

Prefer Node.*_NODE over magic numbers

=== 1 works, but === Node.ELEMENT_NODE is clearer and less error-prone.

#text

Whitespace is TEXT_NODE

Pretty HTML inserts text between tags. First/next child may be type 3, not the element you expect.

deprecated

Ignore unused legacy constants

MDN: ENTITY_REFERENCE_NODE, ENTITY_NODE, and NOTATION_NODE are deprecated and unused. Do not write new code against them.

vs name

nodeType ≠ nodeName

Use nodeType for kind checks; use nodeName for "DIV" / "#text" strings.

Browser Support

Node.nodeType is marked Baseline Widely available on MDN (since July 2015).

✓ Baseline · Widely available

Node.nodeType

Integer kind for every node. Prefer Node.ELEMENT_NODE and friends over raw numbers. A few unused legacy constants are deprecated — not the property itself.

100% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
nodeType Excellent

Bottom line: Safe everyday DOM API. See the MDN nodeType reference for the full constant list.

Performance Notes

ApproachCostBest for
node.nodeType compareO(1)Kind checks in loops
Filter childNodesO(k)Mixed trees with whitespace
parent.childrenO(1) accessElement-only lists (skip filter)

Integer compares are cheap. If you only need elements, prefer children / firstElementChild over scanning every whitespace text node with a type filter.

Key Takeaways

  • Integer kind: classifies element, text, comment, document, …
  • Use constants: Node.ELEMENT_NODE, Node.TEXT_NODE, …
  • Everyday set: 1, 3, 8, 9, 11 cover most HTML work.
  • vs nodeName: number for kind, string for label.

One line: nodeType is the numeric kind of a node — compare with Node.*_NODE constants, not magic numbers.

Frequently Asked Questions

An integer that identifies what kind of node you have — for example 1 for an element (Node.ELEMENT_NODE), 3 for text (Node.TEXT_NODE), or 8 for a comment (Node.COMMENT_NODE).
No. MDN marks Node.nodeType as Baseline Widely available (since July 2015). The property itself is standard. A few old constants (ENTITY_REFERENCE_NODE, ENTITY_NODE, NOTATION_NODE) are deprecated and unused — that does not deprecate nodeType.
Prefer constants such as Node.ELEMENT_NODE or Node.TEXT_NODE. They are clearer than magic numbers like 1 or 3.
nodeType is a numeric kind (element, text, comment, …). nodeName is a string label (DIV, #text, #comment). Use nodeType for kind checks; use nodeName when you need the tag or special name string.
In everyday HTML work: ELEMENT_NODE (1), TEXT_NODE (3), COMMENT_NODE (8), DOCUMENT_NODE (9), and DOCUMENT_FRAGMENT_NODE (11).
Yes. You read nodeType to classify a node; you do not assign to it.

Next: nodeValue

Read or set the string value of text and comment nodes.

nodeValue tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful