JavaScript Array splice() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Add / remove / replace

What You’ll Learn

The splice() method is the Swiss Army knife for in-place array edits: remove, insert, or replace elements at any index. It returns the removed items. This tutorial covers syntax, return values, and how it differs from slice().

01

Syntax

splice(i, n, ...)

02

Remove

deleteCount > 0

03

Insert

deleteCount = 0

04

Replace

Remove + add

05

Returns

Removed []

06

vs slice

Mutates

Introduction

Need to delete two items from the middle, drop in three new ones, or swap a section of an array? splice() does all of that in one call. Unlike slice(), which copies a range without touching the original, splice() mutates the array you call it on.

It also returns a new array of whatever was removed—handy when you need to undo, log, or process deleted items.

Understanding the splice() Method

array.splice(start, deleteCount, item1, item2, ...) begins at start (negative values count from the end), removes deleteCount elements, then inserts any extra arguments at that position.

Omitting deleteCount removes everything from start to the end. Setting deleteCount to 0 inserts without removing.

💡
Beginner Tip

Remember the name pair: slice = copy a slice (safe). splice = splice in changes (mutates). One letter difference, very different behavior.

📝 Syntax

General form of Array.prototype.splice:

JavaScript
array.splice(start, deleteCount, item1, item2, ...)

Parameters

  • start — index to begin (negative = from end).
  • deleteCount — how many elements to remove (0 = insert only).
  • item1, item2, ... — optional elements to insert at start.

Return value

  • Array of removed elements (empty if none removed).

Common patterns

  • arr.splice(2, 1) — remove one item at index 2.
  • arr.splice(1, 0, "new") — insert at index 1.
  • arr.splice(0, 2, "a", "b") — replace first two items.
  • arr.splice(-1, 1) — remove last element.
  • const copy = [...arr]; copy.splice(...) — preserve original.

⚡ Quick Reference

GoalCode
Remove n at indexarr.splice(i, n)
Insert at indexarr.splice(i, 0, ...items)
Replace sectionarr.splice(i, n, ...items)
Remove from index to endarr.splice(i)
Non-mutating (modern)arr.toSpliced(...)
Mutates array?Yes

📋 splice() vs slice() vs push/shift

splice edits anywhere; slice copies; push/shift edit ends only.

splice
any index

Mutates

slice
copy range

Safe

push / pop
end only

Stack

toSpliced
new array

ES2023

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Remove and insert at a specific index.

Example 1 — Remove Elements

Delete two items starting at index 1.

JavaScript
const fruits = ["apple", "banana", "orange", "kiwi"];

const removed = fruits.splice(1, 2);

console.log("removed:", removed);
// ["banana", "orange"]

console.log("fruits:", fruits);
// ["apple", "kiwi"]
Try It Yourself

How It Works

At index 1, remove 2 elements. Return value holds what was deleted; fruits is updated in place.

Example 2 — Insert Without Removing

Set deleteCount to 0 to insert only.

JavaScript
const fruits = ["apple", "kiwi"];

fruits.splice(1, 0, "pineapple", "peach");

console.log(fruits);
// ["apple", "pineapple", "peach", "kiwi"]
Try It Yourself

How It Works

Zero deletions means nothing is removed. New items slide in at index 1, pushing kiwi right.

📈 Practical Patterns

Replace, capture return value, and negative indices.

Example 3 — Replace Elements

Remove two items and insert two different ones in the same spot.

JavaScript
const fruits = ["apple", "banana", "orange", "kiwi"];

fruits.splice(1, 2, "grape", "melon");

console.log(fruits);
// ["apple", "grape", "melon", "kiwi"]
Try It Yourself

How It Works

Replace is remove-then-insert in one step. banana and orange are gone; grape and melon take their place.

Example 4 — Return Value = Removed Items

Always capture the return if you need what was deleted.

JavaScript
const ids = [101, 102, 103, 104];

const deleted = ids.splice(2, 1);

console.log("deleted id:", deleted[0]);
// 103

console.log("remaining:", ids);
// [101, 102, 104]
Try It Yourself

How It Works

splice never returns the updated array—it returns only removed elements. The mutated array is the original reference.

Example 5 — Negative Start Index

-1 starts at the last element—remove the final item.

JavaScript
const stack = ["a", "b", "c"];

const last = stack.splice(-1, 1);

console.log("popped via splice:", last);
// ["c"]

console.log("stack:", stack);
// ["a", "b"]
Try It Yourself

How It Works

Negative start counts from the end (-1 = last index). For end-only removal, pop() is simpler; splice shines at any index.

🚀 Common Use Cases

  • Delete from middle — remove a todo at index 3.
  • Insert in middle — add item without rebuilding array.
  • Replace block — swap a run of elements.
  • Trim tailarr.splice(5) drops from index 5 onward.
  • Undo buffer — store return value before re-inserting.
  • React state — prefer immutable patterns; copy then splice if needed.

🧠 How splice() Runs

1

Resolve start

Negative? Count from array length.

Index
2

Remove deleteCount

Collect removed elements into return array.

Delete
3

Insert new items

Shift elements right to make room.

Insert
4

Return removed

Original array is already updated.

📝 Notes

  • Mutates the original array.
  • Returns removed elements, not the updated array.
  • deleteCount = 0 inserts without deleting.
  • Omitting deleteCount removes through end of array.
  • Negative start is supported (counts from end).
  • Do not confuse with slice() (non-mutating copy).
  • ES2023: toSpliced() returns a new array without mutating.

Browser & Runtime Support

Array.prototype.splice() has been available since ES3 and is supported in every browser and Node.js environment.

Baseline · ES3

Array.prototype.splice()

Supported everywhere: Chrome, Firefox, Safari, Edge, IE, and all Node.js versions. Non-mutating toSpliced() requires ES2023 (2023+ browsers).

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.splice() Excellent

Bottom line: One of the oldest, most reliable array methods. Safe in any JavaScript environment.

Conclusion

splice() is the go-to method for in-place array surgery: remove, insert, or replace at any index. Remember it returns what was removed, mutates the original, and pairs conceptually with non-mutating slice().

Next, learn toLocaleString() to format array values for display.

💡 Best Practices

✅ Do

  • Use deleteCount: 0 for pure inserts
  • Capture return value when you need removed items
  • Copy with spread if immutability matters
  • Use negative start for “from end” edits
  • Prefer pop/shift for end-only ops

❌ Don’t

  • Confuse splice with slice
  • Expect return value to be the updated array
  • Mutate shared arrays without telling teammates
  • Forget that indices shift after each splice
  • Overuse splice in React—spread + filter is often clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.splice()

Edit arrays in place at any index.

5
Core concepts
🗑 02

Remove

deleteCount > 0.

Delete
03

Insert

deleteCount = 0.

Add
📦 04

Returns

Removed [].

Return
05

vs slice

Mutates.

Compare

❓ Frequently Asked Questions

splice() changes an array in place by removing, inserting, or replacing elements starting at a given index. It returns a new array containing the removed elements (which may be empty).
Yes. splice() modifies the original array. Use slice() or toSpliced() (ES2023) when you need a non-mutating alternative.
An array of the removed elements. If nothing was removed, you get an empty array []. The modified array is the same reference you called splice on.
Set deleteCount to 0: arr.splice(index, 0, newItem1, newItem2). Elements shift right to make room at index.
slice(start, end) extracts a copy without mutating. splice(start, deleteCount, ...items) removes/inserts in the original array and returns removed items. Similar names, opposite immutability.
splice() has been available since ES3 and works in all browsers including very old environments and every Node.js version.
Did you know?

Many developers first learn slice() and splice() together because the names differ by one letter—but slice copies safely while splice mutates. That single-letter trap causes real bugs in production code.

Continue to toLocaleString()

Learn how arrays format their elements as localized strings for display.

toLocaleString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful