The keys method in JavaScript is a built-in function. This method produces an Array Iterator object that contains the keys of an array.
What is Iterator?
An iterator is a construct that monitors its current position as it retrieves items from a collection sequentially, one at a time.
An iterator produces an object that contains two attributes: key and value.
Syntax:
Example
array.keys()
Parameters:
No parameters.
Return value:
It produces a fresh array iterator instance that includes the keys corresponding to each index of the elements within the specified array.
Browser Support:
| Chrome | 38.0 |
|---|---|
Edge |
8 |
| Firefox | 28.0 |
| Opera | 25.0 |
Example
JavaScript TypedArray keys Method
Example
<script type="text/javascript">
// JavaScript to illustrate keys() method
// Creating some typedArrays
var A = new Uint8Array([1, 2, 3, 4, 5]);
var B = new Uint8Array([5, 10, 15, 20]);
a = A.keys()
document.write(a.next().value +"<br>");
b = B.keys()
b.next();
document.write(b.next().value +"<br>");
// expected output: arr[Output:0 1]
</script>
Output: