JavaScript Array keys() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.keys() returns an iterator of each index (0, 1, 2, …). It does not yield element values, and it does not mutate the array. Pair it with values() and entries() when you need values or both.

Remember
array.keys()

["a", "b", "c"].keys()
→ iterator yielding 0, then 1, then 2

for (const i of arr.keys()) { ... }
Array.from(arr.keys())  → [0, 1, 2]

returns an iterator (not an array)
array unchanged
indexes only — not values

Syntax

Call array.keys. Start with this line.

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

Built into JavaScript. Call keys(...) on an array instance — no import required.

Library declarations
JavaScript
array.keys()

Parameters

ParameterDescription
ArrayRequired. Source of indexes

Return Value

Return ValueDescription
*Array Iterator of numbers

JavaScript Examples

Example 1 — Loop Indexes with for...of

Use keys() when you care about the index, not just the value.

JavaScript
const langs = ["JS", "Python", "Java"];

for (const i of langs.keys()) {
  console.log(i + ": " + langs[i]);
}
Try It Yourself

How It Works

  1. Iterator. langs.keys() yields 0, then 1, then 2.
  2. for...of. Each loop step receives the next index.
  3. Read value. Use langs[i] when you need the element too.
  4. No mutation. The array stays unchanged.

Example 2 — Collect Indexes with Array.from

Turn the iterator into a real array of numbers.

JavaScript
const fruits = ["apple", "orange", "banana"];
const indexes = Array.from(fruits.keys());

console.log(JSON.stringify(indexes));  // [0,1,2]
console.log(indexes.length);           // 3
Try It Yourself

How It Works

  1. keys() alone is not an array. You cannot use .map on the iterator directly.
  2. Array.from. Consumes the iterator and builds [0, 1, 2].
  3. Spread alternative. [...fruits.keys()] does the same job.
  4. Useful for. Passing indexes into other array methods.

Example 3 — keys vs values

See what each iterator yields side by side.

JavaScript
const colors = ["red", "green"];

console.log(JSON.stringify(Array.from(colors.keys())));    // [0,1]
console.log(JSON.stringify(Array.from(colors.values())));  // ["red","green"]
console.log(JSON.stringify(Array.from(colors.entries()))); // [[0,"red"],[1,"green"]]
Try It Yourself

How It Works

  1. keys. Only indexes: 0, 1.
  2. values. Only elements: "red", "green".
  3. entries. Pairs: [index, value].
  4. Pick one. Use the iterator that matches what you need in the loop.

Browser & Runtime Support

Array.prototype.keys() is part of ES2015 (ES6). It has been available in evergreen browsers and Node.js for years.

✓ Baseline · ES2015

Array.prototype.keys()

Supported in Chrome 38+, Firefox 28+, Safari 8+, Edge 12+, and Node.js 0.12+. Not available in Internet Explorer without a polyfill.

98% 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 and Node LTS. For IE11, feature-detect with if ("keys" in Array.prototype) or use a classic for loop over indexes.

Frequently Asked Questions

keys() returns a new iterator that yields each index in the array, from 0 to length - 1. It does not yield the element values.
It returns an Array Iterator, not an array. Use for...of to loop, or Array.from(arr.keys()) to collect indexes into a real array.
keys() yields indexes (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 stays the same.
Yes. For sparse arrays, keys() still yields every index from 0 to length - 1, including empty slots.
keys() is ES2015 (ES6). It works in all modern browsers and Node.js. Internet Explorer needs a polyfill.

Next: lastIndexOf()

Find the last position of a value in 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.

11 people found this page helpful