JavaScriptArraysBeginner

Array.slice()

Returns a shallow copy of part of an array without mutating it.

Review the syntaxStudy the examplesOpen the coding app
array.slice(start?, end?)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Returns a shallow copy of part of an array without mutating it.

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. Crucially, the original array remains unmodified, making it a primary tool for immutable data patterns. Performance-wise, slice() is O(n) relative to the number of elements extracted. For reference types, it performs a shallow copy: changes to the internal properties of objects within the sliced array will reflect in the original array because they share the same memory reference. If start is undefined, it defaults to 0; if start is greater than the array length, an empty array is returned. Negative indices are treated as offsets from the end of the array (length + index).

Quick reference

Syntax

array.slice(start?, end?)

Inputs

Parameters

start (optional)number · Start index (inclusive). Defaults to 0.
end (optional)number · End index (exclusive). Defaults to array.length.

See it in practice

Examples

1

Basic Subarray Extraction

const colors = ['red', 'green', 'blue', 'yellow', 'purple'];
const selection = colors.slice(1, 4);
Output:
['green', 'blue', 'yellow']

Extracts elements from index 1 up to, but not including, index 4.

2

Using Negative Indices

const numbers = [10, 20, 30, 40, 50];
const lastThree = numbers.slice(-3);
Output:
[30, 40, 50]

A single negative argument extracts from that offset to the end of the array.

3

Creating a Shallow Copy

const original = [{ id: 1 }, { id: 2 }];
const copy = original.slice();
copy[0].id = 99;
console.log(original[0].id);
Output:
99

Calling slice() with no arguments clones the array. Note that internal object references are shared.

Debug faster

Common Errors

1

LogicError

Cause: Confusing slice() with splice(). slice() does not mutate the original array, whereas splice() does.

Fix: Assign the result of slice() to a new variable to see the changes.

const arr = [1, 2, 3]; arr.slice(0, 1); console.log(arr); // [1, 2, 3] - original unchanged
2

LogicError

Cause: Misunderstanding the 'end' parameter as a length/count instead of an exclusive index.

Fix: Remember that end is the index where the extraction stops, not how many elements to take.

const arr = ['a', 'b', 'c']; const result = arr.slice(1, 2); // returns ['b'], not ['b', 'c']

Runtime support

Compatibility

Node.js, BrowserAll browsersES3+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Returns a shallow copy of part of an array without mutating it.

start: Start index (inclusive). Defaults to 0. end: End index (exclusive). Defaults to array.length.

LogicError: Assign the result of slice() to a new variable to see the changes. LogicError: Remember that end is the index where the extraction stops, not how many elements to take.