JavaScript Node nodeType Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read-only

What You’ll Learn

The Node.nodeType property is an integer that tells you what kind of node you have—element, text, comment, document, and more. Learn the Node.*_NODE constants, nodeType vs nodeName, and five practical examples.

01

Kind

Read-only property

02

Returns

Integer

03

Element

1 / ELEMENT_NODE

04

Text

3 / TEXT_NODE

05

Comment

8 / COMMENT_NODE

06

Status

Baseline widely

Introduction

Not every DOM node is an element. Whitespace creates text nodes; HTML comments are comment nodes. nodeType is the reliable numeric switch for “what is this?”

Prefer named constants such as Node.ELEMENT_NODE instead of bare numbers so your checks stay readable.

💡
Beginner tip

Pair nodeType with nodeName when debugging: type for the kind, name for the label (DIV, #text, …).

Understanding nodeType

MDN: the read-only nodeType property is an integer that identifies what the node is. It distinguishes elements, text, comments, documents, and other kinds.

  • Always an integer for a valid node.
  • Compare with Node.ELEMENT_NODE, Node.TEXT_NODE, and friends.
  • Read-only — you do not assign to nodeType.
  • A few legacy constants are deprecated and unused (see Notes).

📝 Syntax

JavaScript
const type = node.nodeType;

if (type === Node.ELEMENT_NODE) {
  console.log("This is an element");
}

Return value

An integer specifying the node’s type (see the constant table below).

📚 Common nodeType Constants

ConstantValueMeaning
Node.ELEMENT_NODE1An element (<p>, <div>, …)
Node.ATTRIBUTE_NODE2An attribute node
Node.TEXT_NODE3Text inside an element or attribute
Node.CDATA_SECTION_NODE4A CDATA section (XML)
Node.PROCESSING_INSTRUCTION_NODE7A processing instruction (XML)
Node.COMMENT_NODE8An HTML/XML comment
Node.DOCUMENT_NODE9The document itself
Node.DOCUMENT_TYPE_NODE10The doctype (e.g. <!DOCTYPE html>)
Node.DOCUMENT_FRAGMENT_NODE11A DocumentFragment

⚖️ nodeType vs nodeName

node.nodeTypenode.nodeName
ReturnsInteger kindString label
Element example1 (ELEMENT_NODE)"DIV"
Text example3 (TEXT_NODE)"#text"
Best forKind checks / switchesTag / special name strings

⚡ Quick Reference

GoalCode
Read typenode.nodeType
Is element?node.nodeType === Node.ELEMENT_NODE
Is text?node.nodeType === Node.TEXT_NODE
Is comment?node.nodeType === Node.COMMENT_NODE
Document checkdocument.nodeType === Node.DOCUMENT_NODE
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Node.nodeType.

Returns
number

Node kind

Baseline
widely

Since July 2015

Access
read-only

No assignment

Prefer
Node.*_NODE

Constants

Examples Gallery

Examples follow MDN Node.nodeType patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Check element and text nodes with constants.

Example 1 — Element Is ELEMENT_NODE (1)

Created elements report type 1.

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

Node.ELEMENT_NODE equals 1. Comparing to the constant keeps intent clear.

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

Setting textContent creates a text child. That child’s nodeType is Node.TEXT_NODE.

📈 Comments, Documents & Filtering

Detect comments, check the document, skip non-elements in walks.

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

Pretty-printed HTML often starts with a #text node, so the first child may not be the comment you wrote. Always check nodeType (or skip text nodes).

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

The document, its doctype, and document fragments each have their own nodeType constant.

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

Pretty-printed lists include #text between items. Filtering on ELEMENT_NODE keeps only the real tags (or use children / querySelectorAll for element-only lists).

🚀 Common Use Cases

  • Filter childNodes — keep elements; skip whitespace text.
  • Tree walks — branch on element vs text vs comment.
  • Validate structure — assert a child is a comment or doctype.
  • Fragments — detect DOCUMENT_FRAGMENT_NODE before append.
  • Debug with nodeName — log both type and name for clarity.

🧠 How nodeType Works

1

The engine knows the kind

Every node is created as an element, text, comment, document, …

Create
2

Map kind to an integer

Return 1, 3, 8, 9, and so on.

Integer
3

Compare with constants

Your code checks Node.ELEMENT_NODE instead of magic numbers.

Compare
4

Your script branches

Filter, warn, or handle each kind differently.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • The nodeType property is not Deprecated, Experimental, or Non-standard — no status banner required.
  • Deprecated unused constants: ENTITY_REFERENCE_NODE (5), ENTITY_NODE (6), NOTATION_NODE (12). Avoid them; they do not deprecate nodeType itself.
  • Prefer Node.*_NODE constants over bare integers.
  • Related: nodeName, nodeValue, childNodes, nextSibling, JavaScript hub.

Universal Browser Support

Node.nodeType is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Node.nodeType

Safe for production. Prefer Node.*_NODE constants, and remember a few legacy constants are deprecated but unused.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
nodeType Excellent

Bottom line: Use nodeType for kind checks; use nodeName when you need the string label.

Conclusion

Node.nodeType returns an integer kind for every node. Compare with Node.ELEMENT_NODE, Node.TEXT_NODE, and other constants; pair with nodeName when you need the label string.

Continue with nodeValue, nodeName, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Compare with Node.ELEMENT_NODE (and friends)
  • Filter childNodes by type when skipping whitespace
  • Pair with nodeName while debugging
  • Handle document / fragment types when building trees
  • Ignore deprecated unused constants (5, 6, 12)

❌ Don’t

  • Scatter magic numbers without comments
  • Assume every child of an element is an element
  • Assign to nodeType (read-only)
  • Treat deprecated entity/notation constants as current API
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about nodeType

An integer kind for every node.

5
Core concepts
🏷️ 02

Use constants

Node.*_NODE

Style
03

Text & comments

3 and 8

Common
⚖️ 04

vs nodeName

kind vs label

Compare
🔎 05

Filter walks

ELEMENT_NODE

Pattern

❓ 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.
Did you know?

MDN still lists ENTITY_REFERENCE_NODE, ENTITY_NODE, and NOTATION_NODE, but marks them deprecated and unused. Modern tutorials should teach the live constants (1, 2, 3, 4, 7, 8, 9, 10, 11) and skip those three.

Next: nodeValue

Get or set text and comment content—or see null on elements.

nodeValue →

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.

5 people found this page helpful