Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

JavaScript Array keys() Method

Posted on June 14, 2025June 14, 2025 By Admin No Comments on JavaScript Array keys() Method

JavaScript Array keys() method returns a new Array Iterator object containing the keys (or indices) of the elements in the array. It is particularly useful for iterating over an array’s indices.

JavaScript Array keys() Method

Pre-requisites to Learn

  • JavaScript Array

Syntax

arr.keys();

Parameters

The Array.keys() method does not accept any parameters.

Return Value

The keys() method returns a new Array Iterator object that contains the indices (keys) of the array elements. The iterator can be used in loops or converted into an array.

Notes:

  1. This method does not modify the original array.
  2. This method returns the indices of the array.

Examples of JavaScript Array keys() Method

Example 1: The keys() method returns an iterator that can be used to loop through the indices of the array.

let arr = ['apple', 'banana', 'cherry'];

let keysIterator = fruits.keys();

for (let key of keysIterator) {
    console.log(key);
}
// Output:
// 0
// 1
// 2

Example 2: This method combined with Array.from() method to create an array of indices.

let arr = [10, 20, 30, 40];

let keysArray = Array.from(arr.keys());
console.log(keysArray); // Output: [0, 1, 2, 3]

Example 3: Using keys() with Sparse Arrays – the second element is missing, its index (1) is still included in the keys.

let sparseArray = [10, , 30, 40];

let keysIterator = sparseArray.keys();

for (let key of keysIterator) {
  console.log(key);
}
// Output:
// 0
// 1
// 2
// 3

Example 4: The keys() method can be combined with map() or other methods for advanced processing.

let arr = ['apple', 'banana', 'cherry'];

let keyValues = arr.keys();
let result = Array.from(keyValues).map(key => `Index: ${key}`);

console.log(result); // Output: ['Index: 0', 'Index: 1', 'Index: 2']

Supported Browsers

BrowserSupport
Chrome38+
Firefox32+
Safari8+
Edge12+
Opera25+
Internet ExplorerNot supported

Comparison with Other Methods

MethodPurpose
Array.keys() MethodReturns an iterator of array indices (keys).
Array.values() MethodReturns an iterator of array values.
Array.entries() MethodReturns an iterator of [index, value] pairs.

JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array lastIndexOf() Method
Next Post: JavaScript Array includes() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme