Skip to the solution
MediumFrontend6 min read

What is the difference between slice and splice?

273 views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 103 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How would you use Reflect.ownKeys() inside a Proxy to correctly enumerate both string and symbol keys, including non-enumerable ones?

Continue