JavaScript Element tagName Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

In JavaScript, the tagName property of Element is a read-only string: the tag name of the element (MDN). In HTML documents it is always uppercase — for example "DIV" or "IMG".

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use el.tagName. Start with this line.

JavaScript
el.tagName
CallWhat it does
el.tagNamePrimary signature.

Built into the browser. Read or assign el.tagName — no import required.

Library declarations
JavaScript
el.tagName

Parameters

ParameterDescription
localNameOptional. Lowercase for safer string compares
WriteOptional. Create a new element to change type

Return Value

Return ValueDescription
*The current value of el.tagName.

JavaScript Examples

Example 1 — Read tagName

MDN-style: log the tag name of a span.

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

How It Works

  1. Select the element by id.
  2. Read tagName. Returns a string.
  3. In HTML expect uppercase ("SPAN").
  4. In XML/XHTML case matches the original markup (MDN).

Example 2 — Branch on Tag Type

Use uppercase comparisons in HTML documents.

JavaScript
const el = document.getElementById("target");

if (el.tagName === "BUTTON") {
  console.log("It is a button");
} else if (el.tagName === "IMG") {
  console.log("It is an image");
} else {
  console.log(el.tagName);
}
Try It Yourself

How It Works

  1. Compare to uppercase literals in HTML.
  2. Avoid === "button" — that fails for tagName.
  3. Or use localName for lowercase checks.
  4. Read-only: you cannot change the tag this way.

Example 3 — tagName vs localName

See the HTML casing difference side by side.

JavaScript
const img = document.querySelector("img");

console.log(img.tagName);   // "IMG"
console.log(img.localName); // "img"
console.log(img.nodeName);  // "IMG"
Try It Yourself

How It Works

  1. tagName — uppercase in HTML.
  2. localName — lowercase local name (MDN tip for img).
  3. nodeName — same as tagName on elements.
  4. Pick one style and stick to it in comparisons.

Browser Support

Element.tagName is Baseline Widely available (MDN: across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.tagName

Read-only tag name string. Uppercase in HTML documents; original case in XML. Same as nodeName on Element.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
tagName Baseline

Bottom line: Safe everywhere. Remember HTML uppercase, or use localName for lowercase comparisons.

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" (MDN).
No. MDN marks Element.tagName as Baseline Widely available (since July 2015). 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" (MDN).
In HTML, tagName is uppercase ("IMG") while localName is lowercase ("img"). Prefer localName when you want a case-stable lowercase name (MDN).
For Element objects, yes — tagName and nodeName return the same value (MDN).

Next: EventTarget Constructor

Learn the base interface behind addEventListener and dispatchEvent.

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

10 people found this page helpful