JavaScript NamedNodeMap length Property

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

What You’ll Learn

The read-only length property of a NamedNodeMap tells you how many Attr nodes are in the map. Learn the MDN element.attributes example, looping with item(), live updates when attributes change, and how it compares with hasAttributes()—with five examples and try-it labs.

01

Kind

Instance property

02

Returns

Number count

03

Read-only

no setter

04

Live

updates auto

05

Use case

Loop attrs

06

Status

Baseline widely

Introduction

Every element’s attributes property is a live NamedNodeMap. Its length is simply the number of attributes on that element—class, id, data-*, and so on each count as one.

MDN’s example uses a <pre> with class, id, and contenteditable, then reads pre.attributes.length to report how many attributes the element has.

💡
Beginner tip

length is not an array length you can push to. To add attributes, use setAttribute() or setNamedItem()—the count updates on its own.

Understanding the length Property

A read-only instance property on NamedNodeMap that stores the number of objects in the map.

  • Type — a non-negative integer (number).
  • Read-only — you cannot assign to map.length.
  • Live — changes when attributes are added or removed on the element.
  • Common sourceelement.attributes.length.
  • Pair withmap.item(i) for i from 0 to length - 1.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Read length on any NamedNodeMap (usually element.attributes):

JavaScript
namedNodeMap.length

Return value

A number: the count of Attr nodes currently in the map.

Typical pattern (MDN idea)

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;

pre.textContent = `The element contains ${attrMap.length} attributes.`;

⚡ Quick Reference

GoalCode / note
Count attributesel.attributes.length
Any attributes?el.attributes.length > 0
Loop by indexfor (let i = 0; i < map.length; i++)
Last valid indexmap.length - 1
Read-onlyCannot assign to length
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.length.

Type
number

Attr count

Access
read-only

No setter

Live
yes

Auto updates

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NamedNodeMap.length. Labs use live element.attributes on real DOM nodes.

📚 Getting Started

MDN pattern: read the attribute count on an element.

Example 1 — MDN Count on <pre>

Three attributes: class, id, contenteditable.

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;

pre.textContent = `The element contains ${attrMap.length} attributes.`;
Try It Yourself

How It Works

MDN: length is the number of objects stored in the NamedNodeMap.

Example 2 — Loop Every Attribute with length and item()

Classic index loop from 0 to length - 1.

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

length bounds the loop; see the item() tutorial for index access details.

📈 Live Updates & Comparisons

Count changes, empty maps, and boolean checks.

Example 3 — length Updates Live

Adding and removing attributes changes the count immediately.

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

The same NamedNodeMap object reflects mutations—length is always current.

Example 4 — Empty Element Has length of 0

No attributes means zero—not null or undefined.

JavaScript
const empty = document.querySelector("#empty");
const withAttrs = document.querySelector("#demo");

console.log(
  empty.attributes.length,
  withAttrs.attributes.length,
);
Try It Yourself

How It Works

An element with no attributes still has a NamedNodeMap—its length is 0.

Example 5 — length vs hasAttributes()

Both answer “does this element have any attributes?”

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

hasAttributes() is often clearer for a boolean; length when you need the exact count.

🚀 Common Use Cases

  • Count attributes before looping with item(i).
  • Check length > 0 instead of calling hasAttributes().
  • Debug how many attrs an element has after DOM changes.
  • Validate that removeNamedItem lowered the count by one.
  • Teach live collections vs static arrays in the DOM.

🔧 How It Works

1

Get attributes map

element.attributes returns a live NamedNodeMap tied to the element.

Context
2

Read .length

Returns the current number of Attr nodes—read-only, no parameters.

Count
3

Use for loops or checks

Loop 0…length-1 with item(), or test length > 0 for any attrs.

Pattern
4

Stays in sync

setAttribute, removeAttribute, setNamedItem, removeNamedItem—all update length automatically.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Read-only — you cannot set map.length directly.
  • Live — the count changes when attributes are added or removed.
  • Not the same as array.length on Array.from(map) unless you copy first.
  • Related learning: item(), Element.attributes, hasAttributes(), getNamedItem().

Universal Browser Support

NamedNodeMap.length is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NamedNodeMap.length

Read-only count of Attr nodes in the map—live on element.attributes.

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
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.length Excellent

Bottom line: Read element.attributes.length to count attributes and bound item() loops.

Conclusion

NamedNodeMap.length is a read-only count of Attr nodes in the map. Use it on element.attributes to see how many attributes an element has, loop with item(i), or check length > 0 before processing.

Continue with item(), Element.attributes, getNamedItem(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use length to bound item(i) loops
  • Prefer hasAttributes() for simple boolean checks
  • Remember the map is live after DOM changes
  • Use Array.from(map) when you need array methods
  • Re-read length after adding or removing attrs in a loop

❌ Don’t

  • Try to assign map.length = 0 to clear attributes
  • Assume length is cached across async gaps without re-checking
  • Confuse NamedNodeMap length with child node counts
  • Use length when you only need one attribute by name
  • Forget that item(length) is out of range (returns null)

Key Takeaways

Knowledge Unlocked

Five things to remember about length

Read-only attribute count on NamedNodeMap.

5
Core concepts
⚙️02

Read-only

no setter

Access
⚠️03

Live

auto sync

DOM
📄04

Loop

0…length-1

Pattern
🎯05

Baseline

since Jul 2015

Status

❓ 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.
Did you know?

MDN’s length example reads pre.attributes.length on a <pre class="foo" id="bar" contenteditable> element—three attributes, so length is 3.

Explore item()

Use length with item() to walk every attribute by index.

item() method →

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