Javascript forEach with Index

Ole Ersoy
1 min readMar 18, 2020
Image by Gerd Altmann from Pixabay

Scenario

We need access to the index of each value in the array [1,2] .

Approach

[1,2].forEach((value, i)=> {
console.log('%d: %s', i, value);
})

Result

0: 1
1: 2

Demo

--

--