JavaScript NamedNodeMap item() Method

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

What You’ll Learn

The item() method of a NamedNodeMap returns the Attr node at a numeric index. Learn the MDN element.attributes example, how map[i] mirrors item(i), looping with length, and how index lookup differs from getNamedItem()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Attr or null

03

Arg

Numeric index

04

Alias

map[i] numeric

05

Use case

Loop all attrs

06

Status

Baseline widely

Introduction

Every element exposes a live attributes collection—a NamedNodeMap. You can look up one attribute by name with getNamedItem("class"), but sometimes you need to walk every attribute in order.

That is what item(index) is for. Pass 0, 1, 2, and so on to get each Attr node. MDN also notes that map[1] (numeric bracket) calls the same logic as map.item(1).

💡
Beginner tip

Bracket notation has two meanings on a NamedNodeMap: map[0] uses item(0) (index), while map["id"] looks up by attribute name. Keep numbers and strings straight.

Understanding the item() Method

An instance method on NamedNodeMap that returns the attribute node at a zero-based position.

  • index — a number: 0 for the first attribute, 1 for the second, and so on.
  • Returns — an Attr node, or null if index ≥ length.
  • Bracket aliasmap[i] when i is numeric (MDN).
  • Common sourceelement.attributes on any element.
  • Pair withmap.length to loop every attribute.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Call the method on a NamedNodeMap (usually from element.attributes):

JavaScript
namedNodeMap.item(index)
namedNodeMap[index]   // numeric index only — same as item(index)

Parameters

  • index — non-negative integer position in the map (0-based).

Return value

An Attr at that index, or null when the index is out of range.

Typical pattern (MDN idea)

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

pre.textContent = `The attribute map contains:
0: ${attrMap.item(0).name}
1: ${attrMap[1].name}
2: ${attrMap.item(2).name}`;

⚡ Quick Reference

GoalCode / note
First attributeel.attributes.item(0)
Bracket indexel.attributes[1] (numeric)
Loop allfor (let i = 0; i < map.length; i++) map.item(i)
Out of rangeReturns null
By name insteadgetNamedItem("class")
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.item().

Returns
Attr|null

Node or missing

Arg
index

0-based number

Alias
map[i]

Numeric only

Baseline
widely

Since Jul 2015

Examples Gallery

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

📚 Getting Started

MDN pattern: read attribute names by index.

Example 1 — MDN item() and Bracket Index

List attribute names at indices 0, 1, and 2 on a <pre> element.

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

pre.textContent = `The attribute map contains:
0: ${attrMap.item(0).name}
1: ${attrMap[1].name}
2: ${attrMap.item(2).name}`;
Try It Yourself

How It Works

MDN: attrMap[1] with a number is the same as attrMap.item(1).

Example 2 — Loop Every Attribute with length

Walk the map from 0 to length - 1.

JavaScript
const box = document.querySelector("#demo");
const map = box.attributes;
const lines = [];

for (let i = 0; i < map.length; i++) {
  const attr = map.item(i);
  lines.push(`${attr.name}=${attr.value}`);
}

console.log(lines.join(", "));
Try It Yourself

How It Works

item(i) plus length is the classic way to enumerate all attributes.

📈 Edge Cases & Comparisons

Out-of-range indexes and name vs index lookup.

Example 3 — null When Index Is Out of Range

Indexes at or beyond length return null.

JavaScript
const el = document.querySelector("#demo");
const map = el.attributes;

const last = map.item(map.length - 1);
const pastEnd = map.item(map.length);

console.log(last?.name, pastEnd === null);
Try It Yourself

How It Works

MDN: return null when index ≥ length. Always bounds-check in loops.

Example 4 — Numeric [i] vs String ["name"]

Same index path; string keys use name lookup instead.

JavaScript
const map = document.querySelector("#demo").attributes;

const byIndex = map.item(0);
const byBracketIndex = map[0];
const byName = map["id"];

console.log(
  byIndex === byBracketIndex,
  byName.name,
  byName.value,
);
Try It Yourself

How It Works

map[0] and item(0) share the same Attr; map["id"] finds by name.

Example 5 — item() vs getNamedItem()

Position vs name—two ways to reach the same Attr.

JavaScript
const map = document.querySelector("#demo").attributes;

const byIndex = map.item(0);
const byName = map.getNamedItem("id");

console.log(byIndex.name, byName.name, byIndex === byName);
Try It Yourself

How It Works

Use item when you do not know names upfront; use getNamedItem when you do.

🚀 Common Use Cases

  • Enumerate every attribute on an element for debugging or logging.
  • Build a name/value list from element.attributes.
  • Teach how NamedNodeMap mixes index and name access.
  • Serialize attributes when you need live Attr nodes (not just strings).
  • Interop with legacy DOM code that walks maps by index.

🔧 How It Works

1

Get attributes map

Start from element.attributes—a live NamedNodeMap tied to the element.

Context
2

Call item(index)

Pass a zero-based number, or use map[i] with a numeric index (MDN).

Lookup
3

Return Attr or null

Valid index: Attr with .name and .value. Index ≥ length: null.

Result
4

Read attr.name / attr.value

Or loop with for (let i = 0; i < map.length; i++) for every attribute.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • map[i] with a number calls item(i); string keys use name lookup.
  • Index is zero-based; last valid index is length - 1.
  • Order of attributes is not guaranteed to match source markup order in all cases.
  • Related learning: getNamedItem(), getNamedItemNS(), Element.attributes.

Universal Browser Support

NamedNodeMap.item() 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.item()

Returns the Attr at a numeric index—or null when the index is out of range.

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.item() Excellent

Bottom line: Call map.item(index) or map[index] (numeric) on element.attributes to walk attributes by position.

Conclusion

NamedNodeMap.item() returns the Attr at a numeric index, or null when the index is too large. Use it with length to loop every attribute, and remember that numeric map[i] is the same call (MDN).

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

💡 Best Practices

✅ Do

  • Loop with for (let i = 0; i < map.length; i++)
  • Check for null before reading properties
  • Use getNamedItem when you know the attribute name
  • Prefer getAttribute when you only need string values
  • Remember numeric vs string bracket keys

❌ Don’t

  • Assume attribute order always matches HTML source
  • Call item("id")—that is not a valid index
  • Confuse map[0] with map["0"] name lookup
  • Skip bounds checks when probing random indexes
  • Use item when a simple getAttribute suffices

Key Takeaways

Knowledge Unlocked

Five things to remember about item()

Index-based Attr lookup on NamedNodeMap.

5
Core concepts
⚙️02

Arg

index

Params
⚠️03

map[i]

numeric alias

MDN
📄04

Loop

with length

Pattern
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

The Attr node at the given numeric index, or null if the index is greater than or equal to the map's length.
No. MDN marks NamedNodeMap.item() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
MDN notes that map[i] is equivalent to map.item(i) when i is a number. A string key like map['id'] uses name lookup instead.
item(index) finds an attribute by position (0, 1, 2…). getNamedItem(name) finds an attribute by its name string.
item() returns null when index is greater than or equal to attributes.length.
When you need to walk every attribute on an element in order—often in a for loop with map.length.
Did you know?

MDN’s item() example prints three attribute names using item(0), bracket index [1], and item(2)—all on the same pre.attributes map.

Explore removeNamedItem()

Remove attributes by name and get the Attr node back.

removeNamedItem() 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