JavaScript Array slice() Method

JavaScript Array slice() Method | The result of the Javascript arr.slice() method is a new array that contains a subset of the original array. The initial is unaltered.

Syntax:– arr.slice(begin, end)

The two parameters that this method accepts are as follows and are specified below:-

  • Start: The starting index from which the part is to be retrieved is defined by this argument. Since 0 is the default start value, the procedure uses begin if this parameter is absent.
  • End: The index up to which the part is to be removed is indicated by this parameter (excluding the end index). The array’s last value is extracted if this argument is not defined because it is the default end value. If the end value exceeds the array’s length, the end value is changed to the array’s size.

Return value: This function provides a new array with a subset of the original array as its value.

Let’s look at an example to understand the array slice method better.

JavaScript Array slice() Method Example

In this case, the array is extracted using the slice() function from the supplied array, starting at index 2 and including all elements with an index lower than 4.

Example-1:- JavaScript Array slice() Method

var arr = [12, 34, 44, 45, 78, 57, 77];
console.log("Original Array: " + arr);

var new_arr = arr.slice(2, 4);
console.log("Extracted Array: " + new_arr);

Output:-

Original Array: 12,34,44,45,78,57,77
Extracted Array: 44,45

Example 2: Because no arguments were submitted to the slice() method, it extracts the entire array from the supplied string and provides it as the solution.

Example-2:- JavaScript Array slice() Method

var arr = [12, 34, 44, 45, 78, 57, 77];
console.log("Original Array: " + arr);

var new_arr = arr.slice();
console.log("Extracted Array: " + new_arr);

Output:-

Original Array: 12,34,44,45,78,57,77
Extracted Array: 12,34,44,45,78,57,77

In this example, the slice() method retrieves the array starting at index 2 and continuing until the end of the array and returns it as the solution.

Example-3:- JavaScript Array slice() Method

var arr = [12, 34, 44, 45, 78, 57, 77];
console.log("Original Array: " + arr);

var new_arr = arr.slice(3);
console.log("Extracted Array: " + new_arr);

Output:-

Original Array: 12,34,44,45,78,57,77
Extracted Array: 45,78,57,77

Example-4:- JavaScript Array slice() Method example to remove the first element from the array

Alternative to the shift() function, we can also use the slice() function to remove the first element from a given array. See more:- How to remove elements from an array based on an index.

var arr = [12, 34, 44, 45, 78, 57, 77];
console.log("Original Array: " + arr);

var new_arr = arr.slice(1);
console.log("Extracted Array: " + new_arr);

Output:-

Original Array: 12,34,44,45,78,57,77
Extracted Array: 34,44,45,78,57,77

Example-4:- JavaScript Array slice() Method example to remove the last element from the array

Alternative to the pop() function we can also use the slice() function to remove the last element from the given array.

var arr = [12, 34, 44, 45, 78, 57, 77];
console.log("Original Array: " + arr);

var new_arr = arr.slice(0, -1);
console.log("Extracted Array: " + new_arr);

Output:-

Original Array: 12,34,44,45,78,57,77
Extracted Array: 12,34,44,45,78,57

The slice() method also can be used to remove elements based on an index from a given array. See more here:- How to remove elements from an array based on an index.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *