Array

Array.prototype.keys

Returns a new array iterator object that contains the keys for each index in the array

Syntax

JavaScript
array.keys()

Return Value

A new iterable iterator object

Examples

Basic Usage
const arr = ['a', 'b', 'c'];
const iterator = arr.keys();
for (const key of iterator) {
  console.log(key);
}
// 0, 1, 2
Practical Example
const sparse = [1, , 3];
const keys = [...sparse.keys()];
console.log(keys); // [0, 1, 2]
Advanced Usage
const arr = ['x', 'y', 'z'];
console.log([...arr.keys()]); // [0, 1, 2]

Understanding Array.prototype.keys

The Array.prototype.keys method in JavaScript returns a new array iterator object that contains the keys for each index in the array. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.

The method signature is array.keys(). When called, it returns a new iterable iterator object. Understanding when and how to use keys() helps you write more expressive, readable code.

Common use cases for Array.prototype.keys include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-values, array-entries, object-keys, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Array.prototype.keys is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Array Methods

Other methods in the Array object

Related Tools

More Array Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.