Skip to content

Instantly share code, notes, and snippets.

@solominh
Created January 20, 2017 18:27
Show Gist options
  • Save solominh/ca9954ee51510434e9e8b560eb5cfe2d to your computer and use it in GitHub Desktop.
Save solominh/ca9954ee51510434e9e8b560eb5cfe2d to your computer and use it in GitHub Desktop.
let a = [1, 2, 3, 4, 5]
for (let v of a) {
if (v == 3) {
a.splice(0, 2); // remove 2 element from index 0
a = undefined; // for..of will have arrayname =a => undefined assignment will not affect for..of
}
console.log(v);
}
console.log(a) // undefined
// Result:
// 1
// 2
// 3
// undefined
// => index will always increase and for..of stop when index >= array.length
// => when remove 2 elements from array when index = 2 (v =3) => next index(=3) >= array.length(=3)
// => remove array elements in for..of is not safe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment