Implement Array.slice

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Array.slice

Approach

Normalize start and end indices, handling undefined defaults and negative values by adding them to the array length. Iterate from the computed start to end, copying each element into a new result array without modifying the original.

Complexity Analysis

Time
O(k)
Space
O(k)

Pattern

Array Polyfill

Why It Works

Slice creates a shallow copy of a portion of the array. Negative indices count from the end, enabling patterns like arr.slice(-3) to get the last three elements without knowing the array length.

Updated Feb 2026