Skip to content

JavaScript Array entries() | Method

  • by

JavaScript Array entries() Method method returns a new Array Iterator object with key/value pairs. This method does not change the original array.

array.entries()

JavaScript Array entries()

A simple example code creates an Array Iterator, and then iterates over the key/value pairs:

<!DOCTYPE html>
<html>
<body>
  <script>
    let words = ["Apple", "Ball", "Cat", "Dog"];

    const w = words.entries();
    console.log(w)

    for (let x of w) {
      console.log(x)
    }
  </script>

</body>
</html>

Output:

JavaScript Array entries

More examples

const array1 = ['a', 'b', 'c'];

const iterator1 = array1.entries();

console.log(iterator1.next().value); //[0, "a"]

console.log(iterator1.next().value);// [1, "b"]

Iterating with index and element

const a = ["a", "b", "c"];

for (const [index, element] of a.entries()) {
  console.log(index, element);
}

Using a for…of loop

const array = ["a", "b", "c"];
const arrayEntries = array.entries();

for (const element of arrayEntries) {
  console.log(element);
}

Do comment if you have any doubts or suggestions on this JS Array method tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *