node.nodeName returns a string naming the current node(MDN). For elements it matches tagName; for text and comments it uses fixed labels like #text and #comment.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard. Pair with nodeType when you need a numeric kind check.
Remember
node.nodeName → string name of the node
HTML element → usually UPPERCASE ("DIV")
XML / SVG → usually lowercase ("circle")
Text → "#text"
Comment → "#comment"
Document → "#document"
Read-only — do not assign to nodeName
Elements: nodeName === tagName
Select a node, read its name, and compare against known strings.
Get a DOM node (element, text, comment, …).
Read node.nodeName.
Compare with strings like "DIV", "#text", or "#comment".
Remember HTML tags are usually uppercase.
Piece
Required?
Role
node
Yes
Any DOM Node
Return
—
String name (never null)
Case
—
HTML elements → uppercase; XML/SVG → lowercase
Pseudocode
Pseudocode
name = node.nodeName
if name is "#text":
// text or whitespace node
else if name is "#comment":
// HTML comment
else if name is "#document":
// the document itself
else:
// usually an element tag ("DIV", "SPAN", "circle", …)
// walk children and list names (MDN-style):
node = parent.firstChild
while node:
print(node.nodeName)
node = node.nextSibling
Reference
Cheat Sheet
Keep these name values handy while inspecting the DOM.
Topic
Detail
Syntax
node.nodeName
Returns
String (always)
Mutates?
No — read-only
HTML elements
Usually uppercase ("DIV")
Text / comment
"#text" / "#comment"
vs tagName
Same on elements; tagName is Element-only
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Pick a node from the demo tree and read its nodeName.
Demo tree
Hello
Compact markup keeps text and comment as direct children of #nn-box.
Result
Choose a target, then Run…
Code
JavaScript Examples
Short, copy-ready snippets based on MDN Node.nodeName. Use Try It Yourself for a live editor.
Same on elements. MDN: element nodeName is tagName.
tagName is Element-only. Text nodes do not have it.
Use nodeName for mixed lists. Safer when walking childNodes.
Use tagName when typed as Element. Clearer intent for UI tags.
Example 5 — List Names While Walking (MDN-style)
Walk siblings and append each nodeName — the classic MDN demo idea.
JavaScript
let node = document.getElementById("sample").firstChild;
let result = "Node names are:\n";
while (node) {
result += `${node.nodeName}\n`;
node = node.nextSibling;
}
console.log(result);
Node names are:
#text
DIV
#text
#comment
#text
SPAN
#text
How It Works
Start at firstChild. May be whitespace #text in pretty HTML.
Append each nodeName. Shows elements, text, and comments.
Advance with nextSibling. Same pattern as MDN’s example.
Stop at null. End of the parent’s child list.
Edge Cases & Pitfalls
Check these before comparing nodeName strings in real code.
case
HTML tags are uppercase
MDN: HTML elements report uppercase names. Comparing to "div" fails — use "DIV" or normalize case.
SVG
XML / SVG are lowercase
SVG and MathML elements typically use lowercase nodeName (e.g. "circle"), unlike HTML tags.
tagName
tagName is Element-only
Text and comment nodes have no tagName. Prefer nodeName when walking mixed childNodes.
#text
Whitespace is still #text
Pretty-printed HTML inserts text nodes between tags. Expect "#text" entries in sibling walks.
Compatibility
Browser Support
Node.nodeName is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.nodeName
Returns a string naming the node. HTML elements are usually uppercase; text and comments use #text and #comment.
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
nodeNameExcellent
Bottom line: Safe everyday DOM API. Remember HTML uppercase vs SVG lowercase. See the MDN nodeName reference.
Analysis
Performance Notes
Approach
Cost
Best for
node.nodeName
O(1)
Quick string identity
node.nodeType
O(1)
Numeric kind checks in hot loops
Walk + collect names
O(k) children
Debugging / teaching mixed trees
Reading nodeName is cheap. For tight filters, nodeType (number compare) can be slightly clearer than string compares. Prefer children / firstElementChild when you only care about elements.
Remember
Key Takeaways
String name: every node has a nodeName you can read.
HTML uppercase: expect "DIV", not "div".
Specials:#text, #comment, #document, and more.
vs tagName: same on elements; use nodeName for mixed node lists.
One line:nodeName is the readable label for any DOM node — uppercase HTML tags, plus fixed names like #text.
Frequently Asked Questions
A string naming the current node. For HTML elements it matches Element.tagName (usually uppercase, like DIV). Special nodes use fixed names such as #text, #comment, and #document.
No. MDN marks Node.nodeName as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
In HTML documents, Element.tagName (and therefore nodeName for elements) is uppercase. In XML/SVG contexts, tag names are typically lowercase.
tagName exists only on Element. nodeName works on every Node. For elements they return the same string; for text or comments, only nodeName applies (#text, #comment).
Common ones: #text, #comment, #document, #document-fragment, and #cdata-section. Attributes use Attr.name; document types use DocumentType.name.
Yes. You read nodeName to inspect a node; you do not assign to it.