Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is array entries() in Javascript?

Samia Ishaque

Overview

The entries function in JavaScript is used to return an array iterator object. entries can print each of the indices and the values present on that index in the array. The general syntax for the entries function is:

array.shift()

Parameters

The entries function takes no mandatory inputs as arguments.

Return value

The entries function returns an object of type array iterator.

Example

The following example will help you understand the entries function and how you can use it.

First, we create the array and call the entries function on it to acquire the array iterator object. Then, we can use the following methods to print the indices and values on those indices.

Using the next().value

You can use the next().value function to acquire the value present at the next index. Each print using console.log will print the next element in the array. If you iterate beyond the range of the array, it prints undefined, as the array is not defined for that index.

Using the for loop

You can also use the for loop method to print each index and its element.

const arr1 = ['1', '2', '3'];

const i1 = arr1.entries();

//method1
console.log('method1')
console.log(i1.next().value);
console.log(i1.next().value);
console.log(i1.next().value);
console.log(i1.next().value);

//method2
console.log('method2')
const i2 = arr1.entries();
for (let item of i2) {
  console.log(item);
}

RELATED TAGS

javascript

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring