JavaScript Reference
Array.prototype.slice()
Returns a shallow copy of a portion of an array. Does not modify the original.
Last updated: October 2026
Definition: Array.prototype.slice() returns a new array containing a shallow copy of a section of the original array. You specify where to start and optionally where to end. The original array is never modified.
🎬 Live Example
Five common slice patterns. Click any button to see the output.
💡 slice never mutates. The original array is safe.
Basic Syntax
array.slice(start, end)
// start — index to begin (inclusive). Default 0.
// end — index to stop (exclusive). Default array.length.
Both arguments are optional. Calling slice() with no arguments creates a full shallow copy.
🎯 Syntax & Parameters
| Parameter | Type | Description |
|---|---|---|
start | number | Index to begin. Negative counts from end. |
end | number | Index to stop (exclusive). Negative counts from end. |
Basic Slicing
const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
fruits.slice(1, 3); // ['banana', 'cherry']
fruits.slice(2); // ['cherry', 'date', 'elderberry']
fruits.slice(); // full copy
fruits.slice(0, 2); // ['apple', 'banana']
Negative Indices
const nums = [10, 20, 30, 40, 50];
nums.slice(-2); // [40, 50] — last two
nums.slice(-3, -1); // [30, 40] — from 3rd-from-end to 1st-from-end
nums.slice(1, -1); // [20, 30, 40] — drop first and last
Pro tip: slice(1, -1) is the fastest way to drop the first and last element without mutation.
Copy an Array
const original = [1, 2, 3];
const copy = original.slice();
copy.push(4);
// original: [1, 2, 3] — unchanged
// copy: [1, 2, 3, 4]
Shallow copy only. For arrays of objects, both arrays share the same object references. Mutating a nested object affects both.
Real-World: Pagination
const items = [...Array(50).keys()]; // 0..49
const pageSize = 10;
const page = 2;
const pageItems = items.slice(page * pageSize, (page + 1) * pageSize);
// [20, 21, 22, ..., 29]
slice is the go-to method for paginating arrays and slicing query results.
Slicing Strings Too
const str = 'JavaScript';
str.slice(0, 4); // 'Java'
str.slice(4); // 'Script'
str.slice(-6); // 'Script'
str.slice(0, -1); // 'JavaScrip'
String.prototype.slice works identically. Works with emojis and Unicode safely unlike substring().
🎯 slice vs splice vs substring
| Method | Mutates? | Use when |
|---|---|---|
slice | No | Copy a portion without changing the original |
splice | Yes | Remove / replace / insert in-place |
substring | No | Strings — but swaps args if start > end |
substr | No | Deprecated — avoid |
⚠️ Common Mistakes
- Thinking slice mutates. It never does. If you wanted mutation, use
splice. - Expecting a deep copy. slice is shallow — nested objects are shared.
- Confusing end index as inclusive. end is exclusive.
slice(0, 3)returns indices 0, 1, 2. - Passing non-numeric args. Numbers get coerced; NaN behaves like 0.
- Using slice for a single element. Consider
arr[0]or destructuring instead. - Assuming slice throws on bad ranges. It returns
[]silently.
❓ Frequently Asked Questions
What does Array.slice() do in JavaScript?
Array.slice() returns a shallow copy of a portion of an array into a new array. It takes a start and optional end index, and does not modify the original array.
What is the difference between slice and splice?
slice returns a new array without modifying the original. splice mutates the original array by removing, replacing, or adding elements. slice is non-destructive, splice is destructive.
Can slice use negative indices?
Yes. Negative indices count from the end. slice(-2) returns the last two elements. slice(1, -1) returns everything except the first and last elements.
Does slice copy objects deeply?
No. slice performs a shallow copy. For arrays of objects, the objects themselves are shared by reference. Use structuredClone() or JSON methods for deep copies.
What happens if end is smaller than start in slice?
slice returns an empty array. It never throws an error for out-of-range or reversed indices.