Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.slice()

BaselineWidely available

Theslice() method ofArray instances returns ashallow copy of a portion ofan array into a new array object selected fromstart toend(end not included) wherestart andend representthe index of items in that array. The original array will not be modified.

Try it

const animals = ["ant", "bison", "camel", "duck", "elephant"];console.log(animals.slice(2));// Expected output: Array ["camel", "duck", "elephant"]console.log(animals.slice(2, 4));// Expected output: Array ["camel", "duck"]console.log(animals.slice(1, 5));// Expected output: Array ["bison", "camel", "duck", "elephant"]console.log(animals.slice(-2));// Expected output: Array ["duck", "elephant"]console.log(animals.slice(2, -1));// Expected output: Array ["camel", "duck"]console.log(animals.slice());// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

Syntax

js
slice()slice(start)slice(start, end)

Parameters

startOptional

Zero-based index at which to start extraction,converted to an integer.

  • Negative index counts back from the end of the array — if-array.length <= start < 0,start + array.length is used.
  • Ifstart < -array.length orstart is omitted,0 is used.
  • Ifstart >= array.length, an empty array is returned.
endOptional

Zero-based index at which to end extraction,converted to an integer.slice() extracts up to but not includingend.

  • Negative index counts back from the end of the array — if-array.length <= end < 0,end + array.length is used.
  • Ifend < -array.length,0 is used.
  • Ifend >= array.length orend is omitted orundefined,array.length is used, causing all elements until the end to be extracted.
  • Ifend implies a position before or at the position thatstart implies, an empty array is returned.

Return value

A new array containing the extracted elements.

Description

Theslice() method is acopying method. It does not alterthis but instead returns ashallow copy that contains some of the same elements as the ones from the original array.

Theslice() method preserves empty slots. If the sliced portion issparse, the returned array is sparse as well.

Theslice() method isgeneric. It only expects thethis value to have alength property and integer-keyed properties.

Examples

Return a portion of an existing array

js
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];const citrus = fruits.slice(1, 3);// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']// citrus contains ['Orange','Lemon']

In this example,slice(1, 3) extracts elements from index1 up to, but not including, index3, resulting in a new array['Orange', 'Lemon'].

Omitting the end parameter

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];const tropical = fruits.slice(2);console.log(tropical); // ['Orange', 'Mango', 'Pineapple']

In this example,slice(2) extracts elements from index2 to the end of the array.

Using negative indices

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];const lastTwo = fruits.slice(-2);console.log(lastTwo); // ['Mango', 'Pineapple']

In this example,slice(-2) extracts the last two elements of the array. When using a negative index with theslice method, negative indices are counted from the end of the array, starting at-1 for the last element,-2 for the second-to-last element, and so on. The negative index-2 itself is included because it is the starting point of the extraction.

|     |     |     |     |     ||  S  |  L  |  I  |  C  |  E  ||     |     |     |     |     |  -5    -4    -3    -2    -1<--- read from reverse

Using a positive start index and a negative end index

js
const fruits = ["Apple", "Banana", "Orange", "Mango", "Pineapple"];// Using positive start index and negative end indexconst sliceExample = fruits.slice(1, -1);console.log(sliceExample); // ['Banana', 'Orange', 'Mango']

In this example,slice(1, -1) starts extracting from index1 and goes up to, but does not include, the element at index-1 (which is the last element). This results in a new array with['Banana', 'Orange', 'Mango']. Theslice method always excludes the element at the final index specified, regardless of whether it is positive or negative.

read from start --->   0     1     2     3     4|     |     |     |     |     ||  S  |  L  |  I  |  C  |  E  ||     |     |     |     |     |  -5    -4    -3    -2    -1<--- read from reverse

Using slice with arrays of objects

In the following example,slice creates a new array,newCar,frommyCar. Both include a reference to the objectmyHonda.When the color ofmyHonda is changed to purple, both arrays reflect thechange.

js
// Using slice, create newCar from myCar.const myHonda = {  color: "red",  wheels: 4,  engine: { cylinders: 4, size: 2.2 },};const myCar = [myHonda, 2, "cherry condition", "purchased 1997"];const newCar = myCar.slice(0, 2);console.log("myCar =", myCar);console.log("newCar =", newCar);console.log("myCar[0].color =", myCar[0].color);console.log("newCar[0].color =", newCar[0].color);// Change the color of myHonda.myHonda.color = "purple";console.log("The new color of my Honda is", myHonda.color);console.log("myCar[0].color =", myCar[0].color);console.log("newCar[0].color =", newCar[0].color);

This script writes:

myCar = [  { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } },  2,  'cherry condition',  'purchased 1997']newCar = [ { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }, 2 ]myCar[0].color = rednewCar[0].color = redThe new color of my Honda is purplemyCar[0].color = purplenewCar[0].color = purple

Calling slice() on non-array objects

Theslice() method reads thelength property ofthis. It then reads the integer-keyed properties fromstart toend and defines them on a newly created array.

js
const arrayLike = {  length: 3,  0: 2,  1: 3,  2: 4,  3: 33, // ignored by slice() since length is 3};console.log(Array.prototype.slice.call(arrayLike, 1, 3));// [ 3, 4 ]

Using slice() to convert array-like objects to arrays

Theslice() method is often used withbind() andcall() to create a utility method that converts an array-like object into an array.

js
// slice() is called with `this` passed as the first argumentconst slice = Function.prototype.call.bind(Array.prototype.slice);function list() {  return slice(arguments);}const list1 = list(1, 2, 3); // [1, 2, 3]

Using slice() on sparse arrays

The array returned fromslice() may be sparse if the source is sparse.

js
console.log([1, 2, , 4, 5].slice(1, 4)); // [2, empty, 4]

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.slice

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp