Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • 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 *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme