JavaScript Element getAttributeNames() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.getAttributeNames() method returns an array of strings — every attribute name on the element (MDN). No attributes → an empty array.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.getAttributeNames. Start with this line.

JavaScript
el.getAttributeNames()
CallWhat it does
el.getAttributeNames()Primary signature.
el.getAttributeNames(...)Call with the arguments your case needs.

Built into the browser. Call getAttributeNames(...) on an Element — no import required.

Library declarations
JavaScript
el.getAttributeNames()

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*string[] of attribute names

JavaScript Examples

Example 1 — List Names

Get every attribute name as a string array.

JavaScript
const el = document.getElementById("card");
const out = document.getElementById("out");

out.textContent = el.getAttributeNames().join(", ");
Try It Yourself

How It Works

  1. Select the element. Any Element can be inspected.
  2. Call with no args. Returns a plain string array.
  3. Join for display. Easy to print or log.
  4. Order may vary. Treat it as a set of names, not a fixed sequence.

Example 2 — Names + Values

MDN-friendly pattern: loop names and call getAttribute.

JavaScript
const el = document.getElementById("card");
const out = document.getElementById("out");

const lines = el.getAttributeNames().map((name) => {
  return name + "=" + el.getAttribute(name);
});
out.textContent = lines.join("\n");
Try It Yourself

How It Works

  1. List names first. You only get names from this method.
  2. Read each value. getAttribute(name) returns the string.
  3. Build lines. Map name → name=value.
  4. Why this combo? MDN: more memory-efficient than walking attributes.

Example 3 — Empty Array

An element with no attributes returns [].

JavaScript
const el = document.createElement("div");
const out = document.getElementById("out");

const names = el.getAttributeNames();
out.textContent =
  "length: " + names.length + "\n" +
  "empty: " + (names.length === 0);
Try It Yourself

How It Works

  1. Create a bare element. No attributes in the markup or via JS.
  2. Call the method. Result is always an array.
  3. Check .length. Zero means no attributes.
  4. Not null. Empty list ≠ missing return value.

Browser Support

Element.getAttributeNames() is Baseline Widely available on MDN (across browsers since October 2018). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.getAttributeNames()

List attribute names as a string array. Widely available since 2018.

2018+ Widely available
Google Chrome 61+
Supported
Mozilla Firefox 45+
Supported
Apple Safari 10.1+
Supported
Microsoft Edge 17+
Supported
Opera 48+
Supported
Internet Explorer Not supported
Unavailable
getAttributeNames() Baseline

Bottom line: Safe for modern browsers. Prefer with getAttribute() over walking Element.attributes for simple dumps.

Frequently Asked Questions

It returns an array of strings — the names of all attributes on the element (MDN). If there are no attributes, it returns an empty array.
No. MDN marks Element.getAttributeNames() as Baseline Widely available (across browsers since October 2018). It is not Deprecated, Experimental, or Non-standard.
An array of attribute name strings. Pair with getAttribute(name) in a loop to read each value.
getAttributeNames() returns only name strings. Element.attributes is a live NamedNodeMap of Attr nodes. MDN recommends getAttributeNames() with getAttribute() as a more memory-efficient alternative.
No. Call el.getAttributeNames() with no arguments.
If an attribute has a namespace prefix (for example xlink:href), the prefix is included. Attributes in a namespace without a prefix return just the local name, with no namespace indication (MDN).

Next: getAttributeNode()

Get the Attr node for a named attribute.

getAttributeNode() →

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.

19 people found this page helpful