JavaScript | array.keys()

The array.keys() is an inbuilt function in JavaScript which is used to return a new array iterator which contains the keys for each index in the given input array.

Syntax:

array.keys()

Parameter: It doesn’t take any parameter.

Return Values: It returns a new array iterator.

JavaScript code to show working of array.keys() function:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
   // Taking input as an array A 
   // containing some elements.
   var A = [ 'gfg', 'geeks', 'cse', 'geekpro' ];
  
  // array.keys() function is called
   var iterator = A.keys();
  
  // printing index array using the iterator
   for (let key of iterator) {
    document.write(key + ' ');
   }
</script>

chevron_right


Output:

0 1 2 3 

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Taking input as an array A
  // containing some elements
  var A = [ 'gfg', 'geeks', 'cse', 'geekpro', '', 1, 2 ];
  
  // array.keys() function is called
  var iterator = A.keys();
  
  // printing index array using the iterator
  for (let key of iterator) {
    document.write(key + ' ');
  }
</script>

chevron_right


Output:

0 1 2 3 4 5 6 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.