mardi 13 août 2019

Unexpected Output from forEach()

I was expecting 1,2,3,4 form using the forEach on the array arr with the condition, but got 1,2,3. Why is this so? Doing a regular forEach on the array without the condition gives the expected output: 1,2,3,4

let arr = [1, 2, 3, 4];
arr.forEach(function(i, x, y) {
  if (i in y) {
    document.write(i);
  }
});



Aucun commentaire:

Enregistrer un commentaire