It does not hold any parameter.
It returns a new array iterator object.
Let's see some examples to understand better.
Here's the simple implementation of keys() method in array.Test it Now
So, according to the number of elements present in the array, a key is assigned to each one.
Implementing keys() method with holes in between the array.Test it Now
Note: It is clear from the above example that Array keys() method does not ignore the holes present as an array element in the given array. It assigns a key value to that empty hole too. Also, the keys are assigned in an increasing order sequence to each element.
Example3Test it Now
Here's an example to generate keys for a user-define array.Test it Now
Initially, the user will input the elements through prompt box which will be displayed as:
After clicking on Get Keys button, the keys will be generated as:
Thus, keys() method is a direct approach forgenerating keys for the array values.