JavaScript Array keys()
Example
Create an Array Iterator object, containing the keys of the array:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Create an Iterable
const list = fruits.keys();
// List the Keys
let text = "";
for (let x of list) {
text += x + "<br>";
}
Try it Yourself »
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Create an Iterable
const list = fruits.keys();
// List the Keys
let text = "";
for (let x of list) {
text += x + "<br>";
}
More Examples Below !
Description
The keys() method returns an Iterator object with the keys of an array.
The keys() method does not change the original array.
Array Iteration Methods:
Syntax
array.keys()
Parameters
NONE
Return Value
Type
Description
An array An Array Iterator object containing the keys of an array.
More Examples
Example
Iterate directly over the iterator:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// List the Keys
let text = "";
for (let x of fruits.keys()) {
text += x + "<br>";
}
Try it Yourself »
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// List the Keys
let text = "";
for (let x of fruits.keys()) {
text += x + "<br>";
}
Example
Use the built in Object.keys() method:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// List the Keys
let text = "";
for (let x of Object.keys(fruits)) {
text += x + "<br>";
}
Try it Yourself »
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// List the Keys
let text = "";
for (let x of Object.keys(fruits)) {
text += x + "<br>";
}
Array Tutorials:
Browser Support
keys() is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
| Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
| May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |