JavaScript NodeList keys() Method

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

What You’ll Learn

The keys() method of a NodeList returns an iterator of indexes (unsigned integers). Learn for...of over list.keys(), how it differs from entries() and values(), and how to pair keys with item()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Iterator

03

Yields

Indexes

04

Params

None

05

Loop

for...of

06

Status

Baseline widely

Introduction

A NodeList is ordered. Each node has a position: index 0, 1, 2, and so on. list.keys() gives you an iterator over those index numbers—not the nodes themselves.

MDN’s classic idea: on a list of three child nodes, for (const key of list.keys()) logs 0, then 1, then 2.

💡
Beginner tip

Need the node too? Use entries() for [index, node] pairs, or combine keys() with list.item(key).

Understanding the keys() Method

An instance method on NodeList that returns an iterator over all keys in the list. The keys are unsigned integers (zero-based indexes).

  • Returns — an iterator (not an array).
  • Each step — a number: 0, 1, 2, …
  • Parameters — none.
  • Does not mutate the NodeList or the DOM by itself.
  • Baseline Widely available on MDN (since October 2017).

📝 Syntax

Call the method on a NodeList:

JavaScript
keys()

Parameters

None.

Return value

An iterator. Each yielded value is an unsigned integer index.

Typical pattern (MDN idea)

JavaScript
const list = node.childNodes;

for (const key of list.keys()) {
  console.log(key);
  // 0, 1, 2
}

⚡ Quick Reference

GoalCode / note
Get iteratorlist.keys()
Loop indexesfor (const key of list.keys())
Node at keylist.item(key) or list[key]
To arrayArray.from(list.keys()) or [...list.keys()]
ParametersNone
MDN statusBaseline Widely available (since October 2017)

🔍 At a Glance

Four facts to remember about NodeList.keys().

Returns
iterator

Indexes

Params
none

No arguments

Mutates?
no

Read-only walk

Baseline
widely

Since Oct 2017

Examples Gallery

Examples follow MDN NodeList.keys(). Labs use childNodes and querySelectorAll.

📚 Getting Started

Walk keys the MDN way.

Example 1 — for...of Over keys() (MDN Idea)

Log each index from childNodes.

JavaScript
const node = document.createElement("div");
node.appendChild(document.createElement("p"));
node.appendChild(document.createTextNode("hey"));
node.appendChild(document.createElement("span"));

const list = node.childNodes;

for (const key of list.keys()) {
  console.log(key);
}
Try It Yourself

How It Works

Three child nodes produce three keys: 0, 1, and 2.

Example 2 — Keys + item()

Use each index to fetch the matching node.

JavaScript
for (const key of list.keys()) {
  const node = list.item(key);
  console.log(key, node.nodeName);
}
Try It Yourself

How It Works

keys() gives indexes; item(key) resolves each index to a node.

📈 Selectors & Arrays

Use keys with querySelectorAll and Array.from.

Example 3 — querySelectorAll Keys

Walk indexes for matched list items.

JavaScript
const items = document.querySelectorAll("li");

for (const key of items.keys()) {
  console.log(key, items[key].textContent);
}
Try It Yourself

How It Works

Each key is the position of a matched <li> in the NodeList.

Example 4 — Materialize With Array.from

Turn the keys iterator into a real array of numbers.

JavaScript
const indexes = Array.from(list.keys());
console.log(indexes);
// [0, 1, 2]
Try It Yourself

How It Works

Useful when you need array methods on the index list itself.

Example 5 — keys() vs entries()

Keys yield numbers; entries yield [index, node] pairs.

JavaScript
for (const key of list.keys()) {
  console.log("key", key);
}

for (const [index, node] of list.entries()) {
  console.log("entry", index, node.nodeName);
}
Try It Yourself

How It Works

Pick keys() when you only need indexes; pick entries() when you need both index and node together.

🚀 Common Use Cases

  • Walk indexes with for...of and early break.
  • Pair keys with item() to resolve nodes by index.
  • Build an array of indexes with Array.from.
  • Teach iterator methods alongside Array keys().
  • Compare index-only iteration with entries() and values().

🔧 How It Works

1

Get a NodeList

From childNodes, querySelectorAll, or another DOM API.

DOM
2

Call keys()

list.keys() returns a fresh iterator over unsigned integer indexes.

Call
3

Iterate

for...of (or Array.from) pulls each index until the list is exhausted.

Loop
4

Use the index

Log it, or pass it to item() / bracket access to get the node.

📝 Notes

  • MDN: Baseline Widely available (since October 2017) — no Deprecated / Experimental / Non-standard banner.
  • Returns an iterator; yields indexes, not nodes.
  • Indexes are zero-based unsigned integers.
  • Does not mutate the DOM by itself.
  • Related learning: entries(), item(), forEach(), childNodes.

Universal Browser Support

NodeList.keys() is marked Baseline Widely available on MDN (since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NodeList.keys()

Returns an iterator of zero-based index numbers. Use for...of or Array.from to consume it.

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 No NodeList.keys() (use a polyfill or indexed for loop)
Unsupported
NodeList.keys() Excellent

Bottom line: Call list.keys() when you need indexes only—pair with item() or entries() when you need nodes too.

Conclusion

NodeList.keys() is the index-only iterator for a NodeList. Use for...of when you need position numbers, and combine with item() or entries() when you need the nodes too.

Continue with values(), entries(), item(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use keys() when you only need indexes
  • Pair with list.item(key) to fetch nodes
  • Prefer entries() when you need index + node together
  • Use Array.from(list.keys()) for array methods
  • Remember indexes start at 0

❌ Don’t

  • Expect keys() to yield Node objects
  • Pass arguments to keys()
  • Assume an array return value (it is an iterator)
  • Use keys() when forEach is simpler
  • Forget that childNodes indexes include text nodes

Key Takeaways

Knowledge Unlocked

Five things to remember about NodeList.keys()

Index numbers, as an iterator.

5
Core concepts
⚙️02

Yields

0, 1, 2…

Indexes
🔒03

No params

keys()

Rule
📄04

vs entries

index only

Compare
🎯05

Baseline

since Oct 2017

Status

❓ Frequently Asked Questions

An iterator of unsigned integers—the zero-based indexes of the NodeList. Each step yields a number like 0, 1, 2, not a Node.
No. MDN marks NodeList.keys() as Baseline Widely available (since October 2017). It is not Deprecated, Experimental, or Non-standard.
keys() yields indexes only. values() yields the Node objects. entries() yields [index, node] pairs.
No. It only iterates existing indexes. It does not add, remove, or change nodes by itself.
Yes. After you get index k from keys(), use list.item(k) or list[k] to read the node at that position.
Use keys() when you want index-only iteration with for...of, early break, or Array.from. Use forEach() for simple callbacks on each node.
Did you know?

MDN’s example logs 0, 1, and 2—not nodes. That is the whole point of keys(): unsigned integer indexes you can pass to item() or bracket access.

Explore NodeList.values()

Next up: iterate node objects with for...of.

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