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
Fundamentals
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.
Concept
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 source — element.attributes.length.
Pair with — map.item(i) for i from 0 to length - 1.
Baseline Widely available on MDN (since July 2015).
Foundation
📝 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.`;
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.lengthExcellent
Bottom line: Read element.attributes.length to count attributes and bound item() loops.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about length
Read-only attribute count on NamedNodeMap.
5
Core concepts
🔗01
Type
number
API
⚙️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.