delete vs splice()
delete and splice() can both take an element out of an array, but they do it differently: delete removes only a property and leaves an empty slot, while splice() removes the element, shifts the rest and shrinks length. That is exactly why splice() is the standard tool for arrays, and delete belongs to plain objects.
Theory
TL;DR
delete arr[i]removes a property, not an element: indexes are not shifted andlengthdoes not change.- After
deletethe array keeps an empty slot, which makes it sparse. splice(start, deleteCount)removes elements for real: the rest shifts andlengthis recalculated.splice()returns an array of the removed elements,deleteonly returnstrueorfalse.- Iteration methods (
map,forEach,filter) skip empty slots, which produces unpredictable behaviour. - The rule:
splice()for arrays,deletefor objects.
Quick example
const a = [1, 2, 3];
const b = [1, 2, 3];
delete a[1];
b.splice(1, 1);
console.log(a); // [1, <1 empty item>, 3]
console.log(b); // [1, 3]
console.log(a.length); // 3
console.log(b.length); // 2The same intention, "drop the second element", produces two completely different arrays.
delete: removes the value, but not the array element
const arr = [10, 20, 30];
delete arr[1];
console.log(arr); // [10, <1 empty item>, 30]
console.log(arr.length); // 3What happened:
deleteremoves a property from an object, and an array is an object with numeric keys too.- The position stays empty (an empty slot).
- The indexes are not shifted, and the length (
length) does not change.
The downsides of this approach:
- The array becomes ragged, that is, full of holes.
- Methods such as
.map(),.forEach()and.filter()skip the empty elements. - This often leads to unpredictable behaviour, because
arr[1]still returnsundefinedwhile1 in arrreturnsfalse.
splice(): properly removes the element and shifts the rest
const arr = [10, 20, 30];
arr.splice(1, 1); // remove 1 element starting at index 1
console.log(arr); // [10, 30]
console.log(arr.length); // 2What happened:
splice(start, deleteCount)really removes elements.- All remaining elements are shifted, and the indexes are recalculated.
lengthshrinks automatically.
An extra bonus: the method returns what it removed, and it can insert new values at the same time.
const arr = [10, 20, 30];
const removed = arr.splice(1, 1, 'a', 'b');
console.log(removed); // [20]
console.log(arr); // [10, 'a', 'b', 30]Comparison table
| Criterion | delete | splice() |
|---|---|---|
| Removes the value | Yes | Yes |
| Removes the element itself | No | Yes |
| Changes the array length | No | Yes |
| Shifts the indexes | No | Yes |
| Leaves a hole | Yes | No |
| Returns the removed elements | No, only true or false | Yes, an array of removed elements |
| Mutates the original array | Yes | Yes |
| Recommended for arrays | No | Yes |
When to use which
- An array, and you need to remove an element:
splice(i, 1). - An array you must not mutate:
arr.filter((_, i) => i !== index), orarr.slice()plus concatenation; both return a new dense array. - The first or last element:
shift()andpop()are shorter thansplice()and also updatelengthcorrectly. - A plain object, and you need to drop a key:
delete obj.key, which is exactly the case the operator was designed for.
const user = { name: 'Maria', age: 25 };
delete user.age;
console.log(user); // { name: 'Maria' }Conclusion:
deletemerely wipes a cell and leaves a hole, whilesplice()removes the element for real and rebuilds the array. So for arrays always reach forsplice(), and usedeleteonly for plain objects.
Common mistakes
- Deleting an array element with
deleteand expecting the array to get shorter.lengthnever changes. - Checking for a hole with
arr[i] === undefined. That cannot tell an empty slot from a realundefinedvalue; usei in arrorObject.hasOwn(arr, i). - Forgetting the second argument of
splice(). The callarr.splice(1)removes everything from index1to the end, not a single element. - Confusing
splice()withslice().slice()does not mutate the array and removes nothing, it only returns a copy of a section. - Removing elements in a left to right loop with
splice(). Every removal shifts the indexes, so walk the array backwards or usefilter(). - Expecting
deleteto return the removed value. It only returnstrueorfalse, so save the value beforehand.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.