Why delete leaves a hole in a JavaScript array instead of removing the element
Using delete on an array element removes the property but keeps the array's length, leaving a hole rather than undefined. Running the code in Node.js 22 showed the length stays at 3 and the index is no longer present. Array methods handle these holes inconsistently: forEach and Object.keys skip the gap, while a plain for loop still visits that index. The hole becomes null once the array passes through JSON.stringify, which quietly changes the data's shape in APIs or databases. The article recommends splice or filter for real removal, and reserves delete for the rare case where a sparse array is intended.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in