JS Built-ins
← Array182 / 226
ArrayArray.prototype.splicebeginner

Array.prototype.splice() β€” remove an element

πŸ“– Usage Example
const arr = [1, 2, 3, 4, 5]
arr.splice(1, 2)  // β†’ [2, 3]  (removed)
arr  // β†’ [1, 4, 5]

Use splice(start, deleteCount) to remove elements from an array in place.

Array.prototype.splice()

Array.prototype.splice(start, deleteCount, ...items) mutates the array by removing and/or inserting elements. It returns an array of the removed elements.

Challenge: Implement removeAt(arr, index) that removes 1 element at index and returns the mutated array.

removeAt([1, 2, 3, 4], 1) // β†’ [1, 3, 4]
removeAt([10, 20, 30], 0) // β†’ [20, 30]
πŸ’‘ Hints (2)
  • β€’ `splice(index, 1)` removes exactly one element at the given index.
  • β€’ `splice` mutates the array in place β€” no need to reassign.