Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

JavaScript Array delete vs. splice: Why the Element Is Still There

In JavaScript, deleting an array index leaves a hole rather than shifting items. Use splice to remove and close the gap, or a copy-producing method to preserve the original.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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.

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

  • forEach() and map() skip holes.
  • for...of and spread produce undefined for a hole.
  • The in operator 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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.