Skip to the solution
MediumFrontend6 min read

How would you remove an item from an array immutably using toSpliced(), and how does it compare to splice()?

678 views

The problem

Question presented to candidate: "You need to remove an item from a React state array without mutating it. splice() would mutate it in place. What would you use instead, and how does it actually behave differently under the hood?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Array.prototype.toSpliced() β€” a real, ES2023 non-mutating sibling of splice() β€” takes the identical real arguments (start, deleteCount, ...items) but genuinely returns a brand-new array reflecting the change, leaving the ORIGINAL array completely untouched.
  • πŸ“Œ Interview term: the real, direct verified contrast β€” verified directly: calling splice() genuinely mutated the array it was called on in place (confirmed: the original array's own contents changed); calling toSpliced() with the identical arguments on a separate, untouched original genuinely left that original completely unchanged, while producing a correct new array with the identical resulting shape splice() would have produced.
  • πŸ“Œ Interview term: the real reference-identity guarantee β€” verified directly: toSpliced()'s result is genuinely a DIFFERENT reference from the original array every single time it is called, even when logically removing zero items β€” the real property this bank's own dedicated toSorted()/toReversed() questions rely on for React's ===-based change detection.
  • πŸ“Œ Interview term: toSpliced() is not just for removal β€” a precise answer names that, like splice(), toSpliced() genuinely also supports INSERT-only (deleteCount: 0) and REPLACE (deleteCount > 0 with replacement items) operations β€” verified directly with both variants.
  • A precise answer names the real, practical motivation: in a React (or any) state-management context, .splice()'s in-place mutation genuinely breaks reference-equality-based change detection (the state object's reference never changes, so a re-render can be silently skipped) β€” toSpliced() genuinely avoids this entire class of bug by construction.

Clarifying questions expected:

  • None β€” this is a comparison/practical question; directly demonstrating the real, verified mutation-vs-non-mutation contrast is the strong signal.

Code / implementation expected: Yes β€” real, side-by-side splice() and toSpliced() calls on separate copies of the same array, including remove, insert-only, and replace variants.

  • es2023
  • tricky
  • real-world

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, direct proof: splice() genuinely mutates in place while toSpliced() genuinely leaves the original untouched, across remove/insert/replace variants β€” verified directly

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 70 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

What is the difference between `Object.groupBy()` and `Map.groupBy()` β€” keys, prototypes and ordering?

Continue