JavaScript NamedNodeMap length Property

Beginner
5 min read
Updated: Nov 2026
5 examples
Property

Definition

NamedNodeMap.length is a read-only number: how many objects (attributes) are stored in the map. Per MDN, on elements you almost always read element.attributes.length.

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use map.length. Start with this line.

JavaScript
namedNodeMap.length
CallWhat it does
namedNodeMap.lengthPrimary signature.

Built into the browser. Read map.length on a NamedNodeMap — no import required.

Library declarations
JavaScript
namedNodeMap.length

Parameters

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

Return Value

Return ValueDescription
*The current value of map.length.

JavaScript Examples

Example 1 — Basic length

Count attributes on an element (MDN-style).

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;
console.log(attrMap.length);
// e.g. 3 for class, id, contenteditable
Try It Yourself

How It Works

  1. Select the element. Any Element has an attributes map.
  2. Read length. One number — how many Attrs are stored.
  3. No assignment. You cannot write map.length = 0.
  4. Count only. Names/values come from item() or getNamedItem().

Example 2 — Loop with item()

Use length as the loop bound and visit every attribute.

JavaScript
const box = document.querySelector("#demo");
const map = box.attributes;
const names = [];
for (let i = 0; i < map.length; i++) {
  names.push(map.item(i).name);
}
console.log(map.length, names.join(", "));
Try It Yourself

How It Works

  1. Bound the loop. i runs from 0 to length - 1.
  2. Fetch each Attr. map.item(i) returns one attribute.
  3. Collect names. Push attr.name (or use value).
  4. Print. Count plus the name list.

Example 3 — Live Updates

length changes when you add or remove attributes.

JavaScript
const box = document.querySelector("#demo");
const map = box.attributes;
const before = map.length;
box.setAttribute("title", "Demo box");
const afterAdd = map.length;
box.removeAttribute("class");
const afterRemove = map.length;
console.log(before, afterAdd, afterRemove);
Try It Yourself

How It Works

  1. Snapshot before. Initial attribute count.
  2. Add one. setAttribute increases length by 1.
  3. Remove one. removeAttribute decreases it by 1.
  4. Same map object. The NamedNodeMap is live — no re-query needed.

Example 4 — Empty Element

An element with no attributes has length 0.

JavaScript
const empty = document.querySelector("#empty");
const withAttrs = document.querySelector("#demo");
console.log(empty.attributes.length);     // 0
console.log(withAttrs.attributes.length); // e.g. 2
Try It Yourself

How It Works

  1. Plain element. <div id="empty"> still has an attributes map.
  2. Count is 0. No Attr nodes stored.
  3. Compare. An element with attrs has length > 0.
  4. Safe to read. Never null — always a number.

Example 5 — vs hasAttributes()

length > 0 matches hasAttributes() for “any attrs?”.

JavaScript
const empty = document.querySelector("#empty");
const demo = document.querySelector("#demo");
console.log(
  empty.attributes.length > 0,
  empty.hasAttributes(),
  demo.attributes.length > 0,
  demo.hasAttributes()
);
Try It Yourself

How It Works

  1. Empty element. Both checks are false.
  2. Element with attrs. Both checks are true.
  3. Same question. “Does this element have any attributes?”
  4. Prefer clarity. Use hasAttributes() for a yes/no; use length when you need the count.

Browser Support

NamedNodeMap.length is marked Baseline Widely available on MDN (since July 2015). It is a browser DOM API — not a Node.js built-in.

✓ Baseline · Widely available

NamedNodeMap.length

Supported across modern Chrome, Firefox, Safari, and Edge for counting attributes on an element.

98% Browser support
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
length Excellent

Bottom line: Safe for production. Read attributes.length for the count; use setAttribute/removeAttribute to change it.

Frequently Asked Questions

A number: how many Attr nodes are stored in the map. On elements, element.attributes.length is the count of attributes on that element.
No. MDN marks NamedNodeMap.length as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. length is read-only. Add or remove attributes with setAttribute, removeAttribute, setNamedItem, removeNamedItem, and similar APIs—the count updates automatically.
Yes. element.attributes is a live NamedNodeMap. When attributes change, length reflects the new count without creating a new object.
Loop from 0 to length - 1 and call map.item(i) to visit every attribute—see the item() tutorial for the full pattern.
attributes.length > 0 is equivalent to element.hasAttributes() for checking whether any attributes exist.

Next: Text()

Create DOM text nodes with the Text constructor.

Text() →

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.

11 people found this page helpful