Learn JavaScript
❯ arrays.slice_splice

slice vs splice in JavaScript

$concept

Two similar names, opposite contracts: slice(start, end) copies a range into a new array (non-mutating, end-exclusive, negatives count from the end), while splice(start, deleteCount, ...items) mutates in place — deleting, inserting, and replacing — returning the removed elements.

Mnemonic: slice with a C copies a section; splice with a P performs surgery in place. Confusing them either mutates shared arrays unexpectedly or wonders why nothing changed — the most common array-method mix-up after sort's string default.

$how it works
1

slice copies out

arr.slice(1, 3) returns elements 1–2 in a new array — original untouched, negatives index from the end.

2

splice operates in place

arr.splice(1, 2, "x") deletes two from index 1, inserts "x", and returns the removed pair.

3

Returns differ

slice returns the extracted section; splice returns the removed elements (insertions stay in the array).

4

C copies, P performs

sliCe → Copy; sPlice → Place surgery — the mnemonic that ends the confusion permanently.

$example
let a = [1, 2, 3, 4];
console.log(a.slice(1, 3), a.length);
console.log(a.splice(1, 2), a.length);
Output
[ 2, 3 ] 4
[ 2, 3 ] 2
1slice copies indexes 1–2 out; the original keeps all 4 elements.
2splice removes indexes 1–2 from the array itself (returning them), leaving length 2.
3Same brackets, opposite effects — copy versus surgery demonstrated.
$use cases
Non-destructive subarrays with slice (plus negative indexes)
In-place removal/insertion with splice
Cloning via slice() with no arguments
Remembering C-copies/P-performs under review pressure

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.