JS Built-ins
← Array167 / 226
ArrayArray.prototype.slicebeginner

Array.prototype.slice() β€” basic slice

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

Use slice(start, end) to extract a sub-array without modifying the original.

Array.prototype.slice()

Array.prototype.slice(start, end) returns a shallow copy of a portion of an array from start (inclusive) to end (exclusive). It does not mutate the original.

Challenge: Implement getSlice(arr, start, end) that returns arr.slice(start, end).

getSlice([1, 2, 3, 4, 5], 1, 3) // β†’ [2, 3]
getSlice([10, 20, 30], 0, 2)    // β†’ [10, 20]
πŸ’‘ Hints (2)
  • β€’ `slice(start, end)` does not include the element at `end`.
  • β€’ The original array is never mutated β€” `slice` always returns a new array.