JavaScript Element tagName Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

Element.tagName is a read-only instance property that returns the element’s tag name as a string. In HTML documents that value is always uppercase (for example "DIV" or "IMG"). Learn the HTML vs XML casing rules, how tagName compares to localName and nodeName, and practice with five examples and try-it labs.

01

Kind

Instance property

02

Access

Read-only

03

Type

String

04

HTML

Always uppercase

05

XML

Preserves case

06

Status

Baseline widely

Introduction

Every element has a tag—<div>, <span>, <img>, and so on. element.tagName tells you that tag as a string so you can branch logic, debug markup, or filter nodes without parsing HTML yourself.

The most common beginner surprise: in an HTML page, tagName is uppercase even when your source file used lowercase tags.

JavaScript
const span = document.createElement("span");
console.log(span.tagName); // "SPAN" in HTML
💡
Beginner tip

Compare with uppercase strings (el.tagName === "DIV"), or use localName when you prefer lowercase names like "div".

Understanding the Property

MDN: the read-only tagName property of the Element interface returns the tag name of the element on which it is called. Capitalization depends on the document type.

  • HTML documents — always canonical uppercase ("DIV", "IMG").
  • XML / XHTML trees — preserve the case from the original markup.
  • Same as nodeName — for Element objects the values match.
  • Related: localName — lowercase local name in HTML ("img").

📝 Syntax

JavaScript
element.tagName

Value

A string indicating the element’s tag name. In HTML it is uppercase; in XML it keeps the original case.

ItemDetail
Typestring
AccessRead-only
HTML example<div>"DIV"
XML example<SomeTag>"SomeTag"
Equalselement.nodeName (for elements)
⚠️
Case-sensitive checks

el.tagName === "div" is false in HTML. Use "DIV", or compare el.localName === "div" instead.

📋 tagName vs localName Pattern

MDN highlights that an <img> reports tagName as "IMG", while localName is "img". Both describe the same element—different casing conventions.

JavaScript
const img = document.createElement("img");
console.log(img.tagName);   // "IMG"
console.log(img.localName); // "img"
console.log(img.nodeName);  // "IMG" (same as tagName for elements)

Related learning: localName, slot, and Node.nodeName.

⚡ Quick Reference

GoalCode / note
Read tag nameel.tagName
HTML checkel.tagName === "BUTTON"
Lowercase nameel.localName === "button"
Same as nodeNameel.tagName === el.nodeName
Cannot renameProperty is read-only
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.tagName.

Kind
get only

Instance

Type
string

Tag name

HTML
UPPERCASE

Canonical

Baseline
widely

DOM API

Examples Gallery

Examples follow MDN Element: tagName and show reading names, casing, comparisons, and branching.

📚 Getting Started

Read a tag name from the page, just like MDN’s span demo.

Example 1 — Read tagName from a Span

Look up an element and log its tag name.

JavaScript
const span = document.getElementById("born");
console.log(span.tagName); // "SPAN" in HTML
Try It Yourself

How It Works

In HTML documents the returned string is uppercase regardless of how the tag was written.

Example 2 — Lowercase Markup, Uppercase Result

Creating "div" still yields "DIV" for tagName.

JavaScript
const box = document.createElement("div");
console.log(box.tagName);   // "DIV"
console.log(box.localName); // "div"
Try It Yourself

How It Works

Use this pair when teaching or debugging casing differences between APIs.

📈 Compare & Branch

Safe comparisons and real UI decisions based on the tag.

Example 3 — Compare Safely in HTML

Uppercase checks and localName both work; mixed case does not.

JavaScript
const btn = document.createElement("button");
console.log(btn.tagName === "BUTTON"); // true
console.log(btn.tagName === "button"); // false
console.log(btn.localName === "button"); // true
Try It Yourself

How It Works

Pick one convention and stick to it in your project to avoid silent bugs.

Example 4 — Branch on Element Type

Use tagName to choose different handling for different tags.

JavaScript
function describe(el) {
  if (el.tagName === "IMG") return "image";
  if (el.tagName === "A") return "link";
  return "other: " + el.tagName;
}

console.log(describe(document.createElement("img")));
console.log(describe(document.createElement("a")));
console.log(describe(document.createElement("p")));
Try It Yourself

How It Works

Handy for event delegation, sanitizers, and accessibility helpers that treat tags differently.

Example 5 — Support Snapshot

Feature-detect and remember the HTML uppercase rule.

JavaScript
console.log({
  supported: "tagName" in Element.prototype,
  returns: "string (UPPERCASE in HTML)",
  tip: "Compare with \"DIV\" or use localName",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Safe everywhere modern browsers run. It is one of the oldest Element identity APIs.

🚀 Common Use Cases

  • Checking whether an event target is a specific tag during delegation.
  • Filtering a NodeList to keep only certain element types.
  • Debugging generated markup by logging each node’s tag.
  • Building simple HTML sanitizers or transformers.
  • Comparing tagName with localName when teaching DOM casing rules.

🔧 How It Works

1

The element has a qualified name

Created from markup or createElement().

Create
2

Document type sets casing

HTML uppercases; XML keeps original case.

Case
3

tagName returns that string

Read-only — same value as nodeName on elements.

Read
4

You branch or filter with confidence

Compare uppercase in HTML, or use localName.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • Read-only — you cannot rename an element by assigning to tagName.
  • In HTML, expect uppercase; in XML/XHTML, expect original case.
  • Related: localName, slot, JavaScript hub.

Browser Support

Element.tagName is Baseline Widely available (MDN). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.tagName

Read-only — returns the element's tag name string (UPPERCASE in HTML).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes
tagName Baseline

Bottom line: Use tagName to identify element types. In HTML compare uppercase strings, or prefer localName for lowercase checks.

Conclusion

tagName is the classic way to ask “what kind of element is this?” Remember the HTML uppercase rule, and reach for localName when lowercase comparisons feel clearer.

Continue with localName, slot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Compare with uppercase in HTML ("DIV", "IMG")
  • Use localName when you want lowercase checks
  • Treat tagName and nodeName as equal on elements
  • Document your team’s preferred comparison style
  • Log tagName when debugging unexpected nodes

❌ Don’t

  • Write el.tagName === "div" in HTML (it fails)
  • Try to assign el.tagName = "SPAN" (read-only)
  • Assume XML documents also uppercase names
  • Confuse tagName with CSS selectors or class names
  • Skip casing tests when writing shared utilities

Key Takeaways

Knowledge Unlocked

Five things to remember about tagName

Read-only string identity for an element’s tag, uppercase in HTML.

5
Core concepts
📝02

string

tag identity

Type
🔍03

UPPERCASE

in HTML

Case
04

Baseline

widely available

Status
🎯05

vs localName

lowercase twin

Tip

❓ Frequently Asked Questions

It returns the element's tag name as a string. In HTML documents that string is always uppercase, for example "DIV" or "IMG".
No. MDN marks Element.tagName as Baseline Widely available. It is not Deprecated, Experimental, or Non-standard.
Yes. You cannot assign a new tag name with tagName. To change the element type you must create a different element.
For HTML DOM trees the browser returns the canonical upper-case form. Markup like <div> still reports "DIV".
In HTML, tagName is uppercase ("IMG") while localName is lowercase ("img"). Prefer localName when you want a case-stable lowercase name.
For Element objects, yes — tagName and nodeName return the same value.
Did you know?

Custom elements still report an uppercase tagName in HTML—for example <my-card> becomes "MY-CARD". Hyphens stay; only the letters are uppercased.

Previous: slot

Learn how Element.slot reads and sets named shadow DOM slot assignments.

← slot

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