JavaScript NodeList keys() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

NodeList.keys() returns an iterator of the list’s zero-based indexes (0, 1, 2, …). Use it with for...of when you need positions, not the nodes themselves (MDN).

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call nodeList.keys. Start with this line.

JavaScript
nodeList.keys()
CallWhat it does
nodeList.keys()Primary signature.
nodeList.keys(...)Call with the arguments your case needs.

Built into the browser. Call keys(...) on a NodeList — no import required.

Library declarations
JavaScript
nodeList.keys()

Parameters

ParameterDescription
keys()Required. Returns the index iterator

Return Value

Return ValueDescription
*Iterator of unsigned integers

JavaScript Examples

Example 1 — for...of over keys()

MDN pattern: log every index in a NodeList.

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

  1. Build a NodeList. Three children → indexes 0 … 2.
  2. Call keys(). You get an iterator of those indexes.
  3. Loop with for...of. Each step logs a number: 0, then 1, then 2.
  4. No nodes yet. To read a child, use list.item(key) (next example).

Example 2 — Pair keys() with item()

Use each index to fetch the node and print its nodeName.

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()) {
  const child = list.item(key);
  console.log(key, child.nodeName);
}
Try It Yourself

How It Works

  1. Iterate indexes. Same keys() loop as Example 1.
  2. Resolve the node. list.item(key) returns the child at that position.
  3. Inspect nodeName. Elements show tag names; text shows #text.
  4. When to prefer entries(). If you always need both index and node, entries() is shorter.

Example 3 — Collect Keys with Array.from

Turn the iterator into a real array of indexes.

JavaScript
const buttons = document.querySelectorAll("button");
const indexes = Array.from(buttons.keys());

console.log(indexes);
console.log(indexes.length === buttons.length);
Try It Yourself

How It Works

  1. Select nodes. querySelectorAll("button") returns a NodeList (demo page has two buttons).
  2. Convert the iterator. Array.from(buttons.keys()) builds [0, 1, …].
  3. Length matches. One key per node — same count as buttons.length.
  4. Why arrays help. You can use array helpers (map, filter) on the indexes afterward.

Browser Support

NodeList.keys() is Baseline Widely available (MDN, since October 2017). It works across modern desktop and mobile browsers.

✓ Baseline · Widely available

NodeList.keys()

Iterator of zero-based NodeList indexes for for...of and Array.from.

Wide Modern browser support
Google Chrome Supported · modern versions
Full support
Mozilla Firefox Supported · modern versions
Full support
Apple Safari Supported · modern Safari / iOS
Full support
Microsoft Edge Supported · Chromium Edge
Full support
Opera Follow Chromium
Full support
Internet Explorer No NodeList.keys()
Not supported
keys() Widely available

Bottom line: Safe for modern web apps. Use for...of or Array.from to consume the iterator. Internet Explorer does not support NodeList iterator methods.

Frequently Asked Questions

An iterator of unsigned integers — the zero-based indexes of the NodeList (0, 1, 2, …). Each step yields a number, not a Node (MDN).
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.
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 a simple callback on each node.

Next: values()

Learn how to iterate the Node objects in a NodeList.

values() tutorial →

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.

9 people found this page helpful