JavaScript Node nodeName Property

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is nodeName?

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
GoalPrefer
Name any nodenode.nodeName
Element tag onlyelement.tagName
Numeric kindnode.nodeType

How to Use nodeName

Select a node, read its name, and compare against known strings.

  1. Get a DOM node (element, text, comment, …).
  2. Read node.nodeName.
  3. Compare with strings like "DIV", "#text", or "#comment".
  4. Remember HTML tags are usually uppercase.
PieceRequired?Role
nodeYesAny 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

Cheat Sheet

Keep these name values handy while inspecting the DOM.

TopicDetail
Syntaxnode.nodeName
ReturnsString (always)
Mutates?No — read-only
HTML elementsUsually uppercase ("DIV")
Text / comment"#text" / "#comment"
vs tagNameSame on elements; tagName is Element-only
StandardBaseline Widely available (since July 2015)

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…

JavaScript Examples

Short, copy-ready snippets based on MDN Node.nodeName. Use Try It Yourself for a live editor.

Example 1 — HTML Element Name Is Uppercase

A div in an HTML document reports "DIV".

JavaScript
const d1 = document.getElementById("d1");
console.log(d1.nodeName); // "DIV"
Try It Yourself

How It Works

  1. Select the element. getElementById("d1") returns a div.
  2. Read nodeName. For elements this matches tagName.
  3. HTML uppercases tags. Even if the source wrote <div>.
  4. Compare carefully. Check "DIV", not "div", in HTML docs.

Example 2 — Text Nodes Are #text

Whitespace or character data nodes use the fixed name #text.

JavaScript
const p = document.getElementById("para");
console.log(p.firstChild.nodeName); // "#text" (pretty-printed HTML)
Try It Yourself

How It Works

  1. Pretty HTML inserts text. Newlines/spaces before a child tag become a Text node.
  2. firstChild is often #text. Not the inner <span>.
  3. Fixed label. Text nodes always report "#text".
  4. No tagName. Only nodeName works on text nodes.

Example 3 — Comments Are #comment

HTML comments are nodes too — their name is fixed.

JavaScript
const wrap = document.getElementById("wrap");
const comment = wrap.firstChild; // <!-- Example of comment -->
console.log(comment.nodeName); // "#comment"
Try It Yourself

How It Works

  1. Comments are real nodes. They live in childNodes.
  2. Read nodeName. Always the string "#comment".
  3. Useful while walking. Filter comments when listing mixed children.
  4. MDN mapping: Comment → "#comment".

Example 4 — nodeName vs tagName

On elements they match; on text only nodeName exists.

JavaScript
const el = document.getElementById("d1");
console.log(el.nodeName); // "DIV"
console.log(el.tagName);  // "DIV"

const text = el.firstChild;
console.log(text.nodeName); // "#text"
// text.tagName → undefined (Element-only)
Try It Yourself

How It Works

  1. Same on elements. MDN: element nodeName is tagName.
  2. tagName is Element-only. Text nodes do not have it.
  3. Use nodeName for mixed lists. Safer when walking childNodes.
  4. 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);
Try It Yourself

How It Works

  1. Start at firstChild. May be whitespace #text in pretty HTML.
  2. Append each nodeName. Shows elements, text, and comments.
  3. Advance with nextSibling. Same pattern as MDN’s example.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
nodeName Excellent

Bottom line: Safe everyday DOM API. Remember HTML uppercase vs SVG lowercase. See the MDN nodeName reference.

Performance Notes

ApproachCostBest for
node.nodeNameO(1)Quick string identity
node.nodeTypeO(1)Numeric kind checks in hot loops
Walk + collect namesO(k) childrenDebugging / 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.

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.

Next: nodeType

Learn the numeric constants that identify each node kind.

nodeType tutorial →

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.

9 people found this page helpful