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
Fundamentals
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, …).
Concept
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).
Foundation
📝 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).
Reference
📚 Common nodeType Constants
Constant
Value
Meaning
Node.ELEMENT_NODE
1
An element (<p>, <div>, …)
Node.ATTRIBUTE_NODE
2
An attribute node
Node.TEXT_NODE
3
Text inside an element or attribute
Node.CDATA_SECTION_NODE
4
A CDATA section (XML)
Node.PROCESSING_INSTRUCTION_NODE
7
A processing instruction (XML)
Node.COMMENT_NODE
8
An HTML/XML comment
Node.DOCUMENT_NODE
9
The document itself
Node.DOCUMENT_TYPE_NODE
10
The doctype (e.g. <!DOCTYPE html>)
Node.DOCUMENT_FRAGMENT_NODE
11
A DocumentFragment
Compare
⚖️ nodeType vs nodeName
node.nodeType
node.nodeName
Returns
Integer kind
String label
Element example
1 (ELEMENT_NODE)
"DIV"
Text example
3 (TEXT_NODE)
"#text"
Best for
Kind checks / switches
Tag / special name strings
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read type
node.nodeType
Is element?
node.nodeType === Node.ELEMENT_NODE
Is text?
node.nodeType === Node.TEXT_NODE
Is comment?
node.nodeType === Node.COMMENT_NODE
Document check
document.nodeType === Node.DOCUMENT_NODE
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 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
Hands-On
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
Pretty-printed lists include #text between items. Filtering on ELEMENT_NODE keeps only the real tags (or use children / querySelectorAll for element-only lists).
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
nodeTypeExcellent
Bottom line: Use nodeType for kind checks; use nodeName when you need the string label.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about nodeType
An integer kind for every node.
5
Core concepts
🔢01
Integer kind
1, 3, 8, …
API
🏷️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.