How to delete array elements in Javascript

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

To delete array elements in Javascript, you can either use delete or splice.  The difference is that use of delete leaves “holes” in the array …

delete array elements

delete array elements

This removes the “c” element.  The index of the subsequent elements remains as before.

Using splice shifts the array elements …

using splice to delete array elements

using splice to delete array elements

The “c” element is removed and the subsequent elements are shifted to fill that hole.  Hence their array indices change.  And that is why myArray[2] is returning something.