GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • MDN
  • HTML: Markup language
  • Elements
  • Global attributes
  • Attributes
  • See all…
  • Responsive images
  • HTML cheatsheet
  • Date & time formats
  • See all…
  • SVG
  • MathML
  • XML
  • CSS: Styling language

This view

  • Original page
  • New address

GeezerVison is showing developer.mozilla.org — Array.prototype.keys() - JavaScript | MDN

Array.prototype.keys()

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

  • See full compatibility
  • Learn more

The keys() method of Array instances returns a new array iterator object that contains the keys for each index in the array.

Try it

const array = ["a", "b", "c"];
const iterator = array.keys();

for (const key of iterator) {
  console.log(key);
}

// Expected output: 0
// Expected output: 1
// Expected output: 2

Syntax

js
keys()

Parameters

None.

Return value

A new iterable iterator object.

Description

When used on sparse arrays, the keys() method iterates empty slots as if they have the value undefined.

The keys() method is generic. It only expects the this value to have a length property and integer-keyed properties.

Examples

Using keys() on sparse arrays

Unlike Object.keys(), which only includes keys that actually exist in the array, the keys() iterator doesn't ignore holes representing missing properties.

js
const arr = ["a", , "c"];
const sparseKeys = Object.keys(arr);
const denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]

Calling keys() on non-array objects

The keys() method reads the length property of this and then yields all integer indices between 0 and length - 1. No index access actually happens.

js
const arrayLike = {
  length: 3,
};
for (const entry of Array.prototype.keys.call(arrayLike)) {
  console.log(entry);
}
// 0
// 1
// 2

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-array.prototype.keys

Browser compatibility

See also

  • Polyfill of Array.prototype.keys in core-js
  • es-shims polyfill of Array.prototype.keys
  • Indexed collections guide
  • Array
  • Array.prototype.entries()
  • Array.prototype.values()
  • Array.prototype[Symbol.iterator]()
  • TypedArray.prototype.keys()
  • Iteration protocols

Help improve MDN

Learn how to contribute

This page was last modified on Oct 26, 2026 by MDN contributors.

View this page on GitHub • Report a problem with this content
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.