delete arr[index] removes an array index as a property, but it does not shorten the array or move later items. The index becomes a hole. To remove an item and close the gap, use arr.splice(index, 1).
What JavaScript’s delete operator actually does
Arrays are objects with indexed properties and a length value. Deleting an indexed property removes that property; it does not compact the array. MDN puts it plainly: “When you delete an array element, the array length is not affected.” MDN: delete – JavaScript
As an Amazon Associate I earn from qualifying purchases.
const items = ["a", "b", "c"];
delete items[1];
console.log(items); // ["a", empty, "c"]
console.log(items.length); // 3
console.log(1 in items); // false
console.log(items[1]); // undefined
The second position is now an empty slot, or hole. Reading the slot produces undefined, but that does not mean the array contains a value there: the indexed property is absent.
How to tell a hole from an explicit undefined
An array slot assigned undefined still exists. A deleted slot does not. Use the in operator to check whether an index is present:
#1 Best Overall
const hole = ["a", "b", "c"];
delete hole[1];
const present = ["a", undefined, "c"];
console.log(hole[1]); // undefined
console.log(present[1]); // undefined
console.log(1 in hole); // false
console.log(1 in present); // true
Both indexed reads return undefined, so the read alone cannot tell you whether the slot is missing. For debugging, check index in array alongside array.length. MDN: Indexed collections – JavaScript
Use splice() when you want to remove an item
splice() changes the array itself: it removes the requested portion, shifts later indexed items down, and updates length. It also returns an array containing the removed portion.
Rank #2
const items = ["a", "b", "c"];
const removed = items.splice(1, 1);
console.log(items); // ["a", "c"]
console.log(items.length); // 2
console.log(removed); // ["b"]
Here, the first argument is the starting index and the second is the number of items to remove. Unlike delete, splice() is a mutating method: the original array changes. MDN: Array – JavaScript
Choose a removal method based on whether the array should change
| Approach | Effect | Changes original? | Best fit |
|---|---|---|---|
delete arr[i] |
Removes the indexed property, leaving a hole; length stays the same. | Yes | Rare cases where an absent property is specifically intended. |
arr.splice(i, 1) |
Removes one item and shifts later items down; length decreases. | Yes | Remove an item by index and update the existing array. |
arr.filter((_, index) => index !== i) |
Creates a filtered array without the selected index. | No | Remove by index while retaining the original array. |
arr.toSpliced(i, 1) |
Creates a new array with the selected item removed. | No | Use a splice-like operation without mutating the original; check runtime support. |
For example, if the index to remove is already known, a filtered copy can be made like this:
const nextItems = items.filter((_, index) => index !== removedIndex);
If values can repeat, remove by a known index or stable identifier rather than relying on indexOf(value) to select the intended occurrence. toSpliced() is the copy-producing counterpart to splice(); verify that the JavaScript runtime you target supports it. MDN: Array – JavaScript
What holes mean for iteration and array methods
A hole does not behave identically across all ways of working with an array. Indexed reads produce undefined, while some methods skip missing indices altogether. That difference can make a callback run fewer times than the array’s length suggests.
Rank #4
forEach()andmap()skip holes.for...ofand spread produceundefinedfor a hole.- The
inoperator reports whether the indexed property exists.
const values = ["a", "b", "c"];
delete values[1];
values.forEach(value => console.log(value));
// Logs "a" and "c"
console.log([...values]);
// ["a", undefined, "c"]
These distinctions are documented in MDN’s guide to indexed collections and its Array reference. MDN: Indexed collections – JavaScript · MDN: Array – JavaScript
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchQuick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




