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
Fundamentals
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).
Concept
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).
Foundation
📝 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
}
for (const key of list.keys()) {
console.log("key", key);
}
for (const [index, node] of list.entries()) {
console.log("entry", index, node.nodeName);
}
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.
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 ExplorerNo 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.
Wrap Up
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.