JavaScript Array entries()

Posted by

Method

Introduction to JavaScript Array entries() Method

The JavaScript Array entries() method returns an array iterator object that contains the key/value pairs for each item in the array. The key/value pairs are stored in an array of arrays. This method can be used to iterate through all the elements in an array.

Syntax

The syntax for the JavaScript Array entries() method is as follows:

array.entries()

Parameters

This method does not take any parameters.

Return Value

The JavaScript Array entries() method returns an array iterator object that contains the key/value pairs for each item in the array.

Examples

Let’s look at an example to see how the JavaScript Array entries() method works:


var colors = ["red", "green", "blue"];

var colorsIterator = colors.entries();

for (let item of colorsIterator) {
console.log(item);
}

In the above example, we create an array called colors which contains three strings “red”, “green”, and “blue”. We then create an iterator object by calling the entries() method on the colors array. We use a for-of loop to iterate through the iterator object and print out the key/value pairs. The output of the code above would be:


[0, "red"]
[1, "green"]
[2, "blue"]

As you can see, the entries() method returns an array iterator object that contains the key/value pairs for each item in the array.

Conclusion

The JavaScript Array entries() method returns an array iterator object that contains the key/value pairs for each item in the array. This method can be used to iterate through all the elements in an array. It is a useful tool for working with arrays in JavaScript.