Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › slice()

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.

Input:

                
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
startnumberIndex to begin. Negative counts from end.
endnumberIndex to stop (exclusive). Negative counts from end.
1

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']
2

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.

3

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.

4

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.

5

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
sliceNoCopy a portion without changing the original
spliceYesRemove / replace / insert in-place
substringNoStrings — but swaps args if start > end
substrNoDeprecated — avoid

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All JavaScript Reference