The Node.nodeName property returns a string naming the current node. Learn HTML uppercase tags vs SVG lowercase names, special values like #text and #comment, and how nodeName compares to tagName.
01
Kind
Read-only property
02
Returns
String
03
HTML tags
Usually UPPERCASE
04
Text
#text
05
Comment
#comment
06
Status
Baseline widely
Fundamentals
Introduction
Every DOM node has a name string. For elements that name is the tag (like DIV). For text and comments, the DOM uses fixed labels such as #text and #comment.
You will see nodeName often in debugging and in sibling walks—pair it with nextSibling to print what each node is.
💡
Beginner tip
Comparing tag names? Prefer case-insensitive checks or element.matches("div") rather than assuming nodeName === "div" (HTML returns "DIV").
Concept
Understanding nodeName
MDN: the read-only nodeName property returns the name of the current node as a string. The exact value depends on the node type.
Element — same as Element.tagName (HTML: usually uppercase).
Text — "#text".
Comment — "#comment".
Document — "#document"; fragment — "#document-fragment".
Attr — the attribute’s qualified name (Attr.name).
Foundation
📝 Syntax
JavaScript
const name = node.nodeName;
console.log(name); // e.g. "DIV", "#text", "#comment"
Return value
A string naming the node (never null for a valid node).
Reference
📚 Common nodeName Values
Node kind
Typical nodeName
HTML element
DIV, SPAN, P, … (uppercase)
SVG / XML element
Often lowercase, e.g. circle
Text node
#text
Comment
#comment
Document
#document
DocumentFragment
#document-fragment
Compare
⚖️ nodeName vs tagName
node.nodeName
element.tagName
Available on
Every Node
Only Element
For elements
Same string as tagName
Tag name string
For text / comments
#text / #comment
Not available
Best for
Mixed node walks
Element-only code
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read name
node.nodeName
HTML tag check (safe)
node.nodeName.toLowerCase() === "div"
Is text node?
node.nodeName === "#text"
Is comment?
node.nodeName === "#comment"
Element only
element.tagName (same value as nodeName)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Node.nodeName.
Returns
string
Node name
Baseline
widely
Since July 2015
Access
read-only
No assignment
HTML tags
UPPERCASE
Usually
Hands-On
Examples Gallery
Examples follow MDN Node.nodeName patterns. Use View Output or Try It Yourself for each case.
Text nodes do not have a tag. The DOM labels them with the string "#text" so you can tell them apart from elements.
📈 Comments, tagName & Walking
Identify comments, compare APIs, list names like MDN.
Example 3 — Comments Are #comment
HTML comments are nodes too—their name is fixed.
JavaScript
const wrap = document.getElementById("wrap");
const comment = wrap.childNodes[1]; // after a leading #text in pretty HTML
console.log(comment.nodeName); // "#comment"
Use nodeName when the list may include non-elements. Use tagName when you already know you have an Element.
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.nodeName 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.nodeName
Safe for production. Remember HTML uppercase tag names and fixed labels like #text and #comment.
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
nodeNameExcellent
Bottom line: Use nodeName for any node type; use tagName when you already have an Element.
Wrap Up
Conclusion
Node.nodeName returns a string naming the node—uppercase HTML tags, #text, #comment, and more. Prefer case-safe comparisons for elements, and use nodeName whenever a walk may include non-element nodes.
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.
Did you know?
MDN’s sample walks the body’s children and prints every nodeName. That is one of the clearest ways to see how browsers insert #text nodes for whitespace between tags, comments, and elements.