Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

JavaScript slice() vs splice(): Copy a Range or Edit in Place?

slice() copies a range into a new array; splice() edits the original and returns removed elements. See how their arguments differ and when to use toSpliced().
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use slice() to copy a portion of an array without changing it; use splice() to remove, insert, or replace elements in the original array. The easiest way to keep them straight is to check whether you want a new selection or an in-place edit.

How slice() and splice() differ

Method Changes the original array? Arguments Returns Use it to
slice(start, end) No Start and end positions; the end position is excluded A new shallow array containing the selected elements Copy or read a portion of an array
splice(start, deleteCount, ...items) Yes Start position, number of elements to remove, and optional items to insert An array of the removed elements Edit an array in place
toSpliced(start, skipCount, ...items) No Start position, number of elements to remove, and optional items to insert A new array with the edit applied Make a splice-like edit without changing the original

The similar names can be misleading, so focus on the effects: slice() selects; splice() changes. This is a mnemonic for these methods, not a general rule for every JavaScript method with a similar name.

As an Amazon Associate I earn from qualifying purchases.

What slice() does

slice() returns selected elements in a new array and leaves the source array unchanged. In slice(start, end), end is an index position, not a number of items to take, and that position is excluded.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const colors = ["red", "green", "blue", "purple"];

const middle = colors.slice(1, 3);
// middle: ["green", "blue"]
// colors: ["red", "green", "blue", "purple"]

The call starts at index 1 and stops before index 3, so it includes indexes 1 and 2.

Slice() index behavior

  • With no arguments, slice() returns a shallow copy of the whole array.
  • If end is omitted or undefined, the selection continues to the end.
  • Negative indexes count backward from the end. A start before the beginning is treated as zero; a start at or beyond the array length produces an empty array.
  • If the normalized end position is at or before the normalized start position, the result is empty.

What splice() does

splice() edits the array it is called on. Its second argument is a removal count, unlike the end-position argument in slice(). Any values after that count are inserted at the starting position. The method returns the removed elements, not the edited array.

const colors = ["red", "green", "blue", "purple"];

const removed = colors.splice(1, 2, "gold");
// removed: ["green", "blue"]
// colors:  ["red", "gold", "purple"]

Here, splice(1, 2, "gold") starts at index 1, removes two items, then inserts "gold" at that position.

Splice() argument behavior

  • splice(start) with only a start position removes from that position through the end.
  • splice() with no arguments removes nothing.
  • splice(start, 0, item) inserts an item without deleting anything.
  • A start position beyond the array length inserts at the end. Negative starts count back from the end and are clamped at the beginning.
  • If no elements are removed, the return value is an empty array.

Because the return value contains deleted elements, assigning it to a variable does not save the edited source array. Also, do not infer splice()‘s return behavior from push(): push() returns the array’s new length.

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

Does slice() make an independent copy?

It makes a new array, but the copy is shallow. Primitive values are copied as values; for objects, the new array and the original hold references to the same objects. Changing an object through one reference is therefore visible through the other.

const original = [{ name: "Ari" }];
const copy = original.slice();

copy[0].name = "Sam";
// original[0].name is also "Sam"

The arrays are separate containers, but slice() does not recursively clone the objects inside them.

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

How to edit immutably with toSpliced()

If you want the remove-and-insert behavior of splice() but need to keep the original array unchanged, use toSpliced() where the runtime supports it. It returns a new edited array; the original still contains its previous elements. Unlike splice(), it does not return the removed elements separately. MDN marks toSpliced() as widely available since July 2023.

const months = ["Jan", "March", "April"];
const updated = months.toSpliced(1, 1, "Feb");
// updated: ["Jan", "Feb", "April"]
// months:  ["Jan", "March", "April"]

For the behavior described by MDN, see the slice(), splice(), toSpliced(), and Array references on MDN Web Docs, accessed October 7, 2026.

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 *

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.