Why does delete work for objects but not for arrays?

When I delete an value in array and output the array, that value turns into undefined but it still shows. Whereas if i do the same with a Object , like deleting one of its keys. When I output the object it doesn’t show the deleted value at all. Why is that? What is the difference between the way Object and Array approach this under the hood?

delete will also work for arrays
the difference is that arrays have ordered values
if you delete the item at index 3, the item at index 4 will still be at index 4, so now there is an hole between index 2 and index 4
if you want to delete an item from an array and want the array indexes to be reordered, don’t use delete

it’s actually not undefined, it says empty

image

So if objects are unordered, does that mean they simply skip the deleted values in some sense? Is that a feature of objects?

if you remove a property from an object they don’t have the property anymore

it’s array that are a special kind of objects, they are ordered
if you want to remove an element fron an array and you want the following elements to change index, you need to use a method like splice, not delete

1 Like