Table of contents

JavaScript Array slice() Method

The JavaScript Array.slice() method returns a shallow copy of a portion of an array into a new array. It allows you to extract elements starting from a specified index to another specified index without modifying the original array. The elements between the start and end indices are copied into a new array.

For example, array.slice(start, end) is useful when you need to create a subarray from an existing array.

Syntax

The syntax for the Array.slice() method is:

plaintext
array.slice(start, end)

Parameters

The Array.slice() method accepts two parameters:

  • start (Optional): The index at which to begin extraction. The default is 0. If negative, it starts counting from the end of the array.
  • end (Optional): The index before which to end the extraction. The slice will include elements up to, but not including, end. The default is the length of the array. If negative, it is counted from the end of the array.

Return Value

A new array containing the extracted elements. If no parameters are provided, a shallow copy of the entire array is returned.

Examples of JavaScript Array.slice() Method

Example 1: Slicing a Portion of an Array

You can use the slice() method to extract a portion of an array by specifying a start and end index.

javascript
const fruits = ['apple', 'banana', 'mango', 'orange', 'grape'];
const slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits);  // Output: ['banana', 'mango']

Explanation: The slice() method extracts elements from index 1 to index 3 (excluding 3), resulting in the subarray ['banana', 'mango'].

Example 2: Slicing from a Start Index to the End

If no end index is provided, slice() extracts elements from the start index to the end of the array.

javascript
const numbers = [10, 20, 30, 40, 50];
const slicedNumbers = numbers.slice(2);
console.log(slicedNumbers);  // Output: [30, 40, 50]

Explanation: The slice(2) method extracts elements from index 2 to the end of the array, resulting in [30, 40, 50].

Example 3: Using Negative Indices

You can use negative indices to start slicing from the end of the array.

javascript
const numbers = [10, 20, 30, 40, 50];
const slicedNumbers = numbers.slice(-3);
console.log(slicedNumbers);  // Output: [30, 40, 50]

Explanation: The slice(-3) method starts slicing from the third-to-last element, resulting in the subarray [30, 40, 50].

Example 4: Slicing with Both Positive and Negative Indices

You can mix positive and negative indices to slice a specific portion of an array.

javascript
const numbers = [1, 2, 3, 4, 5, 6];
const slicedNumbers = numbers.slice(1, -2);
console.log(slicedNumbers);  // Output: [2, 3, 4]

Explanation: The slice(1, -2) method starts at index 1 and extracts elements up to (but not including) the second-to-last element, resulting in [2, 3, 4].

Example 5: Creating a Copy of the Entire Array

If no arguments are passed to slice(), it returns a shallow copy of the original array.

javascript
const fruits = ['apple', 'banana', 'mango'];
const copiedFruits = fruits.slice();
console.log(copiedFruits);  // Output: ['apple', 'banana', 'mango']

Explanation: The slice() method with no arguments creates a shallow copy of the fruits array, returning a new array with the same elements.

JavaScript

Related Articles