Search code examples
javascriptarrays

What means an empty element in an array and why it has taken into account in the length if theoretically it does not exist


I add a element to the array in a higher position than its length

enter image description here

I wonder if is there a way to access or delete this empty elements, I think it can waste memory


Solution

  • When doing this, you create a "sparse array". The items in between the non-null entries contain undefined. As such, the memory footprint is negligible. You cannot delete them. If you don't like this behavior, just don't create sparse arrays.