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
Read the integer, compare to a Node.*_NODE constant, then branch.
Get a DOM node.
Read node.nodeType.
Compare with Node.ELEMENT_NODE, Node.TEXT_NODE, etc.
Filter or branch based on the kind.
Piece
Required?
Role
node
Yes
Any DOM Node
Return
—
Integer kind code
Constants
Recommended
Node.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)
Reference
Cheat Sheet
Common constants you will see in HTML and JS DOM work.
Constant
Value
Means
Node.ELEMENT_NODE
1
Element (<p>, <div>, …)
Node.TEXT_NODE
3
Text / whitespace
Node.COMMENT_NODE
8
HTML comment
Node.DOCUMENT_NODE
9
The document
Node.DOCUMENT_TYPE_NODE
10
<!DOCTYPE html>
Node.DOCUMENT_FRAGMENT_NODE
11
DocumentFragment
Also
2, 4, 7
Attribute, CDATA, processing instruction
Try it
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…
Code
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
childNodes includes text. Pretty lists insert whitespace nodes.
Filter by ELEMENT_NODE. Keep only real <li> tags.
Collect nodeName. HTML reports "LI".
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
nodeTypeExcellent
Bottom line: Safe everyday DOM API. See the MDN nodeType reference for the full constant list.
Analysis
Performance Notes
Approach
Cost
Best for
node.nodeType compare
O(1)
Kind checks in loops
Filter childNodes
O(k)
Mixed trees with whitespace
parent.children
O(1) access
Element-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.
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.