by codingdave · 5y ago · 0 comments
That sure seems like a feature, not a bug. I can remove entries from an array, and still iterate the array without having to recreate it - Sure, being aware that .length is incorrect is these cases is good. Likewise, it is good to know that if the nulls are meaningful, you need to use another technique. But that is all the more reason to appreciate that forEach, etc. skips the nulls, while a for loop on .length will process them. Choose whichever is appropriate.
[1,,3].length
// 3
[1,,3].forEach(v => console.log(v))
// 1
// 3
[1,undefined,null,3].forEach(v => console.log(v))
// 1
// undefined
// null
// 3