JavaScript Node nodeName Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read-only

What You’ll Learn

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

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").

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).

📝 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).

📚 Common nodeName Values

Node kindTypical nodeName
HTML elementDIV, SPAN, P, … (uppercase)
SVG / XML elementOften lowercase, e.g. circle
Text node#text
Comment#comment
Document#document
DocumentFragment#document-fragment

⚖️ nodeName vs tagName

node.nodeNameelement.tagName
Available onEvery NodeOnly Element
For elementsSame string as tagNameTag name string
For text / comments#text / #commentNot available
Best forMixed node walksElement-only code

⚡ Quick Reference

GoalCode
Read namenode.nodeName
HTML tag check (safe)node.nodeName.toLowerCase() === "div"
Is text node?node.nodeName === "#text"
Is comment?node.nodeName === "#comment"
Element onlyelement.tagName (same value as nodeName)
MDN statusBaseline Widely available (since July 2015)

🔍 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

Examples Gallery

Examples follow MDN Node.nodeName patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See names for elements, text, and comments.

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

For elements, nodeName matches tagName. In HTML that is usually uppercase even if you wrote lowercase tags in the source.

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

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"
Try It Yourself

How It Works

Comment nodes use "#comment". Walking mixed children often shows #text, elements, and #comment interleaved.

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 (tagName is Element-only)
Try It Yourself

How It Works

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);
Try It Yourself

How It Works

Pretty-printed markup inserts #text between tags and comments. Printing nodeName makes that structure visible.

🚀 Common Use Cases

  • Debug sibling walks — log each node’s name while using nextSibling.
  • Skip text nodes — continue when nodeName === "#text".
  • Tag checks — compare with toLowerCase() for HTML elements.
  • Mixed trees — identify comments vs elements vs text in one loop.
  • Teach the DOM — show why indented HTML creates many #text names.

🧠 How nodeName Works

1

Look at the node type

Element, text, comment, document, attribute, and so on.

Type
2

Pick the name rule

Tag name for elements; fixed strings like #text for others.

Rule
3

Return a string

HTML elements are usually uppercase; SVG/XML often lowercase.

String
4

Your script branches

Compare names, skip #text, or log the tree structure.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • HTML element names are usually uppercase; do not hard-code lowercase without normalizing.
  • Related: nextSibling, nodeType, firstChild, childNodes, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
nodeName Excellent

Bottom line: Use nodeName for any node type; use tagName when you already have an Element.

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.

Continue with nodeType, nextSibling, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Normalize with toLowerCase() for HTML tag checks
  • Use nodeName in mixed sibling walks
  • Treat #text / #comment as first-class names
  • Compare with tagName only on elements
  • Log nodeName when teaching whitespace in the DOM

❌ Don’t

  • Assume HTML tags return lowercase names
  • Call tagName on text or comment nodes
  • Assign to nodeName (read-only)
  • Ignore SVG/XML case differences
  • Confuse DOM Node with the Node.js runtime

Key Takeaways

Knowledge Unlocked

Five things to remember about nodeName

A string name for every node.

5
Core concepts
🔠 02

HTML tags

UPPERCASE

Gotcha
03

#text / #comment

fixed labels

Special
⚖️ 04

vs tagName

Node vs Element

Compare
🔄 05

Walk + log

MDN pattern

Pattern

❓ 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.
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.

Next: nodeType

Classify nodes with ELEMENT_NODE, TEXT_NODE, and more.

nodeType →

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.

5 people found this page helpful