JavaScript Array keys() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Index iterator

What You’ll Learn

The keys() method returns an iterator of array indices (0, 1, 2…). Use it with for...of, convert with Array.from, or compare with entries() and values(). This tutorial covers syntax, five examples, sparse-array behavior, and when a plain index loop is simpler.

01

Syntax

keys()

02

Iterator

Not array

03

Indices

0…n−1

04

for...of

Loop keys

05

Immutable

Read-only

06

ES2015

Modern JS

Introduction

Arrays are indexed collections. Sometimes you need those index numbers explicitly—to read arr[i], build parallel structures, or walk slots in order. The keys() method exposes indices as an iterator.

For many tasks, a classic for loop or arr.forEach((v, i) => ...) is enough. Reach for keys() when you want iterator-style iteration or symmetry with values() and entries().

Understanding the keys() Method

array.keys() returns a new Array Iterator. Each step of the iterator produces the next numeric index. It does not yield element values—only keys.

The iterator is lazy: nothing runs until you consume it with for...of, spread, or Array.from.

💡
Beginner Tip

To get index and value together, use entries() or forEach((value, index) => ...). Use keys() when you only need indices.

📝 Syntax

General form of Array.prototype.keys:

JavaScript
array.keys()

Parameters

  • None.

Return value

  • A new Array Iterator object yielding indices.
  • Original array unchanged.

Common patterns

  • for (const i of arr.keys()) { ... } — loop indices.
  • Array.from(arr.keys()) — collect indices into an array.
  • [...arr.keys()] — same with spread.
  • arr.keys().next().value — first index manually.

⚡ Quick Reference

GoalCode
Iterator of indicesarr.keys()
Loop indicesfor (const i of arr.keys())
Array of indicesArray.from(arr.keys())
Index + value pairsarr.entries()
Mutates array?No

📋 keys() vs values() vs entries() vs index loop

Three iterator methods cover indices, values, or both. Pick the one that matches what you need per step.

keys
0, 1, 2

Indices only

values
a, b, c

Elements only

entries
[i, v]

Index + value

for loop
classic

Full control

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Iterate indices with for...of.

Example 1 — Loop Indices with for...of

Consume the iterator to print each index.

JavaScript
const fruits = ["apple", "orange", "banana"];

for (const key of fruits.keys()) {
  console.log(key);
}
// 0
// 1
// 2
Try It Yourself

How It Works

Each yielded value is the numeric index, not the fruit string. Access values with fruits[key].

Example 2 — Collect Indices with Array.from

Turn the iterator into a regular array of index numbers.

JavaScript
const fruits = ["apple", "orange", "banana"];

const indices = Array.from(fruits.keys());

console.log(indices);
// [0, 1, 2]
Try It Yourself

How It Works

[...fruits.keys()] works the same way. Useful when you need an array of indices for another API.

📈 Practical Patterns

Index + value, iterator family, sparse arrays.

Example 3 — Log Index and Value Together

Use each key to read the matching element.

JavaScript
const fruits = ["apple", "orange", "banana"];

for (const key of fruits.keys()) {
  console.log(`Index: ${key}, Value: ${fruits[key]}`);
}
// Index: 0, Value: apple
// Index: 1, Value: orange
// Index: 2, Value: banana
Try It Yourself

How It Works

This pattern mirrors bracket access. For index+value in one step, entries() is often cleaner.

Example 4 — keys() vs values()

See what each iterator yields on the same array.

JavaScript
const letters = ["a", "b", "c"];

console.log([...letters.keys()]);
// [0, 1, 2]

console.log([...letters.values()]);
// ["a", "b", "c"]
Try It Yourself

How It Works

keys() walks positions; values() walks elements. Same length, different payload.

Example 5 — Sparse Arrays Still Yield Index Keys

Empty slots get an index in keys() but may be undefined in values().

JavaScript
const sparse = ["a", , "c"]; // hole at index 1

console.log([...sparse.keys()]);
// [0, 1, 2]

console.log([...sparse.values()]);
// ["a", undefined, "c"]
Try It Yourself

How It Works

keys() covers every index from 0 to length - 1, including holes. forEach would skip the empty slot; iterators include its index.

🚀 Common Use Cases

  • Index-driven logic — process arr[i] with explicit i.
  • Parallel arrays — align data by index from two sources.
  • Iterator pipelines — compose with other iterables.
  • Sparse inspection — visit every slot including holes.
  • Teaching iterators — pair with values() and entries().
  • Custom algorithms — when you need indices without values first.

🧠 How keys() Runs

1

Create iterator

New Array Iterator bound to the array.

Init
2

Track position

Internal cursor from 0 upward.

Cursor
3

Yield index

Each next() or loop step returns next key.

Produce
4

Done at length

Stops after index length - 1.

📝 Notes

  • Returns an iterator, not an array of keys.
  • Do not confuse with Object.keys() on plain objects.
  • For finding a value’s index, indexOf / findIndex is simpler.
  • Iterator is one-shot; calling again requires a fresh keys().
  • Works on dense and sparse arrays (all indices 0…length−1).
  • ES2015—polyfill or loops for IE11.

Browser & Runtime Support

Array.prototype.keys() was added in ES2015 (ES6) as part of the iterator methods family. It is available in all evergreen browsers and modern Node.js versions.

Baseline · ES2015

Array.prototype.keys()

Supported in Chrome 38+, Firefox 28+, Safari 8+, Edge (all versions), and Node 0.12+. Not available in Internet Explorer without a polyfill.

96% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.keys() Excellent

Bottom line: Safe for modern web apps. For IE11, use classic for loops or a polyfill.

Conclusion

keys() exposes array indices as an iterator. Combine it with for...of or Array.from when you need explicit index access in iterator style.

Next, learn lastIndexOf() to search for the last occurrence of a value from the end of the array.

💡 Best Practices

✅ Do

  • Use for...of to consume the iterator
  • Use Array.from when you need an index array
  • Prefer entries() when you need index and value
  • Remember iterators are single-use
  • Consider findIndex for search tasks

❌ Don’t

  • Expect keys() to return element values
  • Confuse with Object.keys(obj)
  • Reuse a consumed iterator without calling keys() again
  • Use keys loop when indexOf is enough
  • Rely on it in IE11 without a fallback

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.keys()

Iterate array indices with the ES2015 iterator API.

5
Core concepts
🔑 02

Iterator

Not array.

Type
📐 03

Indices

0…n−1.

Yield
🔄 04

for...of

Consume.

Loop
📈 05

entries

Index + value.

Related

❓ Frequently Asked Questions

keys() returns a new iterator object that yields each index (key) in the array, from 0 to length - 1. It does not return the element values.
It returns an Array Iterator, not an array. Use for...of to loop, or Array.from(arr.keys()) to collect indices into an array.
keys() yields indices (0, 1, 2). values() yields the element values. entries() yields [index, value] pairs.
No. keys() only reads the array and returns an iterator. The source array is unchanged.
Yes. For sparse arrays, keys() still yields indices for empty slots between 0 and length - 1.
keys() is ES2015 (ES6). It works in all modern browsers and Node.js. Internet Explorer does not support it without a polyfill.
Did you know?

Arrays are iterable by default—for (const x of arr) loops values, same as values(). keys() is for when you specifically want indices from the iterator protocol.

Continue to lastIndexOf()

Learn how to find the last index of a value when searching from the end of an array.

lastIndexOf() 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.

6 people found this page helpful