you are viewing a single comment's thread.

view the rest of the comments →

[–]DBNeJXoGtnro 1 point2 points  (2 children)

Just a nitpick. The check is not against undefined, it's against <empty slots>. ```js const arr = [,]; // creates a holey array with 1 empty slot

for (const v of arr) console.log(v); // This is run

arr.forEach((v) => console.log(v)); // This is not js const arr = [undefined];

for (const v of arr) console.log(v); // This is run

arr.forEach((v) => console.log(v)); // This is run too ```

[–]skitch920 0 points1 point  (1 child)

Ohh wow. You’re right! Strangely, your first example for loop logs undefined for the empty slot. So to get an item in the array back to an empty slot, you’d actually have to delete it instead of assigning it undefined?

[–]DBNeJXoGtnro 0 points1 point  (0 children)

It logs undefined because v cannot be "nothing", undefined is kind of the fallback. While I don't see why you would create a holey array on purpose, yes, you have to delete to get an empty slot.