How to use JavaScript Array entries()

Program to use JavaScript Array entries() method


let a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ];
let b = a.entries();
for(let c of b){
   console.log(c);
}

Output


[ 0, 'Cat' ]
[ 1, 'Dog' ]
[ 2, 'Tiger' ]
[ 3, 'Lion' ]
[ 4, 'Elephant' ]

Steps Description

  1. Declare three variables a, b and c
  2. Initialize three variables with array value respectively a = [ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ], b = a.entries() and c
  3. Array entries() method returns the index number of the array.
  4. The value of c variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array entries() method


let d = [3,5,7,2,8];
let e = d.entries();
for(let f of e){
   console.log(f);
}

Output


[ 0, 3 ]
[ 1, 5 ]
[ 2, 7 ]
[ 3, 2 ]
[ 4, 8 ]

Steps Description

  1. Declare three variables d, e and f
  2. Initialize three variables with array value respectively d = [3,5,7,2,8], e = d.entries() and f
  3. Array entries() method returns the index number of the array.
  4. The value of f variable is displayed with the help of console.log inbuilt function