JS Built-ins
← Array201 / 226
ArrayArray.prototype.toSplicedintermediate

toSpliced Returns a New Array

πŸ“– Usage Example
const arr = [1, 2, 3]
arr.splice(0, 1)       // mutates arr β†’ [2, 3]
const r = [1,2,3].toSpliced(0, 1)  // safe β†’ [2, 3]

Prefer toSpliced() over splice() in functional code to avoid unintended mutation.

Array.prototype.toSpliced β€” New Reference

While splice mutates and returns removed elements, toSpliced always returns a new array. The result is never the same reference as the input.

Implement toSplicedReturnsNew which returns true if arr.toSpliced(0, 0) is a different reference than arr.

πŸ’‘ Hints (2)
  • β€’ Use strict inequality (!==) to compare references
  • β€’ toSpliced(0, 0) removes nothing but still returns a new array copy