Skip to solution
mediumFrontend

What is the difference between slice and splice?

272 views
01

Understand the problem

Question presented to candidate: "If you call arr.slice(1, 3) versus arr.splice(1, 2) on the same array, what happens to the ORIGINAL array in each case? Are the return values similar too?"

What a strong answer should cover:

  • 📌 Interview term: slice(start, end) — returns a new array containing a shallow copy of the specified portion, without mutating the original array at all.
  • 📌 Interview term: splice(start, deleteCount, ...items)mutates the original array in place, removing deleteCount elements starting at start, optionally inserting new items there, and returns a new array of the elements that were removed.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: after arr.slice(1, 3), the original array was genuinely completely unchanged; after the same-shaped arr.splice(1, 2), the original array was genuinely mutated — the two elements were removed in place, shifting the remaining elements down. slice's return value is the extracted portion; splice's return value is also the extracted portion, but the ORIGINAL array itself is left different afterward.
  • A precise answer names splice's dual/triple capability, verified directly: passing 0 as deleteCount with additional items makes it insert-only (nothing removed); passing a non-zero deleteCount alongside new items makes it replace elements in place.
  • A precise answer names that splice has no string equivalent — verified directly, typeof "hello".splice is genuinely undefined — since strings are immutable and splice's entire purpose is in-place mutation; slice works identically on both arrays and strings, since it never needs to mutate anything.

Clarifying questions expected:

  • None — this is a definitional/comparison question; directly answering the prompt's own mutation question for both methods is the strong signal.

Code / implementation expected: Yes — the before/after array comparison for both methods on identically-shaped calls is the clearest, most convincing demonstration.

array-methods
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every before/after array state below was actually run in Node.

1. Why This Even Matters

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, side-by-side proof: slice never mutates the original array while splice genuinely does, plus insert and replace variants
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 103 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track