JavaScript Array.slice() Method

Usage — The JavaScript Array.slice() method is used to return a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

It has the following syntax:

JavaScript

arr.slice([begin[, end]])

Here is an example of using Array.slice():

JavaScript

var animals = ['Ant', 'Bison', 'Camel', 'Duck', 'Elephant'];

// Output — ["Camel", "Duck", "Elephant"]
console.log(animals.slice(2));

Return Value — This method returns a new array containing the extracted elements.

Additional Information — The Array.slice() method does not alter the original array. It returns a shallow copy of elements from the original array. Elements of the original array are copied into the returned array as follows:

  • For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
  • For strings, numbers and booleans (not String, Number and Boolean objects), slice copies the values into the new array. Changes to the string, number or boolean in one array do not affect the other array.

If start is negative, it is treated as length + start, where length is the length of the array. If end is negative, it is treated as length + end where length is the length of the array. If end is omitted, extraction continues to the end of array. If end occurs before start, no elements are copied to the new array.

Also keep in mind that if a new element is added to either array, the other array is not affected.

Browser Support — The Array.slice() method is available in Chrome, Edge, Firefox, Internet Explorer, Opera and Safari.

Go Directly To — usage, parameters or working examples.

Parameters

begin

The begin parameter is used to specify zero-based index at which to begin extraction. This is an optional parameter and its default value is 0.

A negative index can be used, indicating an offset from the end of the sequence. Fox example, arr.slice(-3) will extract the last three elements in the sequence.

end

The end parameter is used to specify a zero-based index before which to end extraction. For example, arr.slice(1,4) will extract the second element through the fourth element (elements indexed 1, 2, and 3). This is an optional parameter.

You can use a negative index with this method to indicate an offset from the end of the sequence. For example, arr.slice(2,-1) will extract the third element through the second-to-last element in the sequence.

If end is omitted or if its value is greater than the length of the sequence, arr.slice() extracts through the end of the sequence (arr.length).

Working Examples

Here are some examples of using the Array.slice() method:

JavaScript

var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
var citrus = fruits.slice(1, 3);

// Output — ["Banana", "Orange", "Lemon", "Apple", "Mango"]
console.log(fruits);

// Output — ["Orange", "Lemon"]
console.log(citrus);

// Output — ["Orange"]
console.log(fruits.slice(1, -3));

// Output — []
console.log(fruits.slice(3, -2));

// Output — ["Lemon"]
console.log(fruits.slice(2, -2));

// Output — ["Orange", "Lemon", "Apple"]
console.log(fruits.slice(-4, -1));

Further Reading

  1. You can read more about the JavaScript Array.slice() method on MDN.
  2. You can also take a look at the defeinition of JavaScript Array.slice() method in the ECMAScript 2015 specification.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top