I'm reading the delete
unary operator talked about near bottom of page here (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Unary)
It says deleting an item from an array will make the item undefined
. However, as the code below shows, we cannot loop over this undefined
item like we can do with a fresh array with the explicit value of undefined
as one of its items.
Is this just a quirk or is there more to read into here? It doesn't seem meaningful to describe the deleted item as undefined
when it's not treated like undefined
.
let a = [1, 2, 3];
console.log("untampered-with array:", a);
delete a[0];
console.log("array after first item deleted:", a);
a.forEach(element => {
console.log("looping over each item and printing it:", element);
});
b = [undefined, 1, 2];
b.forEach(element => {
console.log("looping over each item of array with undefined explicitly declared:", element);
});
It might also be worth mentioning that in Visual Studio Code, when we print out a
after the first item is deleted, it does not show the first item as undefined (like it does on the Stack Overflow code simulator). It shows this: