Array.splice() Insert, remove or replace
Last updated: 2026-09-09
Array.splice() Insert, remove or replace
splice removes items at an index and optionally inserts new ones, returning the removed items. The original is modified in place.
| Category | Arrays |
|---|---|
| ES version | ES1 (1997) |
📝 Syntax
const removed = array.splice(start, deleteCount, item1, ...);
⚙️ Parameters
| start | Start index; negative counts from the end. |
|---|---|
| deleteCount | Optional. How many to remove; omitted means to the end. |
| itemN | Optional. New elements inserted at that position. |
Returns:An array of removed elements; empty if none were removed.
Mutates the original:Yes (in place)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
[ 2, 3 ]
[ 1, 4, 5 ]
[ 1, 'new', 4, 5 ]
[ 1, 'new', 'replaced', 5 ]
💡 Tip Three forms: insert splice(i, 0, x), remove splice(i, 1), replace splice(i, 1, x).
⚠️ Warning slice copies; splice mutates — a classic interview trap.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| Array.splice() | ✓ v1 | ✓ v12 | ✓ v1.5 | ✓ v3 | ✓ v10.5 |
| Supported by all modern browsers | |||||
🏷️ Related Array Methods
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QWhat does splice return?
AAn array of the removed items; an empty array for pure inserts.
Qsplice vs slice?
Asplice mutates (add/remove/replace); slice copies. Note the different parameter meanings.
Qsplice or pop for the last item?
AUse pop — clearer and faster than splice(nums.length - 1, 1).