JavaScript Code


JavaScript Array keys()


keys() – Iterator containing indices

array.keys();
  • array is an array of elements
  • keys is the array method name

keys() method returns Array Iterator object with keys for each index in the array

Examples

1. get keys iterator object for the fruits array, and print the first two values

let fruits = ['apple', 'banana', 'cherry', 'fig'];
let keys = fruits.keys();
console.log(keys.next().value);
console.log(keys.next().value);

Copyright @2022