Javascript Array Slice (with Example)

In this tutorial, we will learn about the javascript array slice() method with the help of multiple examples.

The javascript array slice() method is used to return a specified portion between two indexes of an array as a new array. For Example, let’s suppose we have an array of length 5, and now we want to extract elements from array indexes 1 to 3. So to do so we can use the javascript array slice() method. 

Example

let numbers = [1, 2, 3, 4, 5];

// returning new sliced array 
let newArray = numbers.slice(1, 3);
console.log(newArray);

// Output: [ 2, 3 ]

Also Read :

Javascript Array Splice
Javascript Array Find
Javascript Array forEach

Array Slice() Syntax

Here is the syntax of the javascript array slice() method;

array.slice(start, end)

Here array is any array.

Array Slice() Parameters

start: starting index from where you want to extract the array portion. If not provided it will automatically start the selection from the very first index of the array, which is 0 (Optional).

end: ending index of the selection. If not provided, it will automatically end the selection at the last index of the array (Optional).

Note: if you will not provide any parameters to the slice() method it will return the original array.

Array Slice() Return Value

This method returns a new array with the extracted array elements.

Example 1: Using the Javascript slice() Method

let companies = ["tata", "mahindra", "ford", "volkswagen", "toyota"];

// return the array from start to end index
let output1 = companies.slice();
console.log(output1);
// Output: [ 'tata', 'mahindra', 'ford', 'volkswagen', 'toyota' ]

// return the array from the second to end index
let output2 = companies.slice(2);
console.log(output2);
// Output: [ 'ford', 'volkswagen', 'toyota' ]

// return the array from the first to fourth index
let output3 = companies.slice(1, 4);
console.log(output3);
// Output: [ 'mahindra', 'ford', 'volkswagen' ]

Output

[ 'tata', 'mahindra', 'ford', 'volkswagen', 'toyota' ]
[ 'ford', 'volkswagen', 'toyota' ]
[ 'mahindra', 'ford', 'volkswagen' ]

Example 2: Using the Javascript slice() Method with Negative index

We can also use negative indexes in javascript. Negative indexes start from the end of the array. The last element of the array will have a negative index of -1, the second last element will have a negative index of -2, and so on.

let companies = ["tata", "mahindra", "ford", "volkswagen", "toyota"];

// return the array from 0 index to second-last index
let output2 = companies.slice(0, -1);
console.log(output2);
// Output:  'tata', 'mahindra', 'ford', 'volkswagen' ]

// return the array after thirs-last index
let output3 = companies.slice(-3);
console.log(output3);
// Output: [ 'ford', 'volkswagen', 'toyota' ]

Output

[ 'tata', 'mahindra', 'ford', 'volkswagen' ]
[ 'ford', 'volkswagen', 'toyota' ]

Example 3: Using the Javascript slice() with Object as the array element

Here we are using an array with an object as the element. So here, what will happen when we extract the array elements into a new array with slice() method, the object reference will also be copied along with it.

This means that when we modify the referenced object in the new array, the changes will be reflected in the original array as well.

let car = { color: 'red', wheels: 4 };

let arr = [car, "bike", "rikshaw", "truck"];
let newArr = arr.slice(0, 2);

//original object
console.log(arr[0]); 

//modifying the object in newArr
//changed the color to blue
newArr[0].color = "blue";

//changes are reflected in original array
console.log(arr[0]);
//Output: { color: 'blue', wheels: 4 }

Output

{ color: 'red', wheels: 4 }
{ color: 'blue', wheels: 4 }

Leave a Comment

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