The Wayback Machine - https://web.archive.org/web/20241231020510/https://www.geeksforgeeks.org/javascript-array-slice-method/
Open In App

JavaScript Array slice() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Array slice() method returns selected elements in an array as a new array. It selects from a given start, up to a (not inclusive) given end. This method does not change the original array, enabling non-destructive extraction of array segments.

Syntax:

arr.slice(begin, end);

Parameters:

  • begin: This parameter defines the starting index from where the portion is to be extracted. If this argument is missing then the method takes begin as 0 as it is the default start value.
  • end: Parameter specifying the end index for extracting a portion from an array, defaulting to array length if undefined, adjusting for exceeding length.

Return value:

This method returns a new array containing some portion of the original array. 

Example 1: Extracting elements between two indexes

Here, the slice() method extracts the array from the given array starting from index 2 and including all the elements less than index 4.

JavaScript
function func() {
    // Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    // Extracted array
    let new_arr = arr.slice(2, 4);
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[ 23, 56, 87, 32, 75, 13 ]
[ 87, 32 ]

Example 2: Passing no arguments

Here, the slice() method extracts the entire array from the given string and returns it as the answer, Since no arguments were passed to it.

JavaScript
function func() {
    //Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    //Extracted array
    let new_arr = arr.slice();
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[ 23, 56, 87, 32, 75, 13 ]
[ 23, 56, 87, 32, 75, 13 ]

Example 3: Extracting array from index 2

In this example, the slice() method extracts the array starting from index 2 till the end of the array and returns it as the answer.

JavaScript
function func() {

    //Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    
    //Extracted array
    let new_arr = arr.slice(2);
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[ 23, 56, 87, 32, 75, 13 ]
[ 87, 32, 75, 13 ]

Example 4: Slicing the nested Array

In this example, the slice() method extracts the elements from the nested array and returns it as the answer.

JavaScript
function func() {

    // Original Array
    let arr = [23, [87, 32, 75, 27,3,10,18 ,13]];
    
    // Extracted array
    let new_arr = arr[1].slice(2, 4);
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[
  23,
  [
    87, 32, 75, 27,
     3, 10, 18, 13
  ]
]
[ 75, 27 ]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers

JavaScript Array slice() Method- FAQs

What does the array slice() method do?

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). The original array is not modified.

How do you use the slice() method to copy an entire array?

To copy an entire array, call slice() without any arguments.

How can you extract a portion of an array using slice()?

To extract a portion of an array, provide the start and end parameters. The method will return a new array containing elements from the start index up to, but not including, the end index.

What happens if you only provide the start parameter?

If you only provide the start parameter, slice() extracts elements from start to the end of the array.

What happens if you provide negative indices?

Negative indices count back from the end of the array. For example, a start of -3 and an end of -1 will select elements starting three from the end and ending one from the end.

How does slice() handle indices that are out of range?

If start or end is out of range, slice() will treat it as if it were within the bounds of the array. It will extract from the nearest valid index.



Next Article

Similar Reads

three90RightbarBannerImg