Last updated: Mar 1, 2024
Reading timeยท4 min
Array.slice()
slice()
Use the Array.shift()
method to remove the first element from an array, e.g.
const firstElement = arr.shift();
.
The Array.shift()
method removes the first element from an array and returns
the removed element.
// โ Remove the first element from an array WITH mutation const arr = ['a', 'b', 'c']; const firstElement = arr.shift(); console.log(firstElement); // ๐๏ธ a console.log(arr); // ๐๏ธ ['b', 'c']
The Array.shift() method removes the first element from the array and returns it.
Array.shift()
method mutates the original array and changes its length.The shift
method removes the array element at index 0
.
If you call the Array.shift()
method on an empty array, the method returns
undefined
.
const arr = []; const firstElement = arr.shift(); console.log(firstElement); // ๐๏ธ undefined console.log(arr); // ๐๏ธ []
slice()
An alternative is to create a copy of the array starting with the second element.
const arr = ['a', 'b', 'c']; const withoutFirst = arr.slice(1); console.log(withoutFirst); // ๐๏ธ ['b', 'c'] console.log(arr); // ๐๏ธ ['a', 'b', 'c']
The Array.slice method returns a copy of a portion of an array.
The method takes the following 2 arguments:
Name | Description |
---|---|
start index | The index of the first element to include in the returned array |
end index | The index of the first element to exclude from the returned array |
When only a single argument is passed to the Array.slice()
method, the slice
goes to the end of the array.
The method returns a shallow copy of the original array containing the elements
from index 1
onwards.
The Array.slice()
method is very different from Array.shift()
because it
doesn't change the contents of the original array.
slice()
You can also use the Array.slice()
method to remove the first N elements from
an array.
You just have to specify a start index of N.
const arr = ['a', 'b', 'c', 'd']; const newArr = arr.slice(2); console.log(newArr); // ๐๏ธ ['c', 'd'] console.log(arr); // ๐๏ธ ['a', 'b', 'c', 'd']
The code sample returns a new array without the first 2 elements of the original array.
Note that the Array.slice()
method doesn't remove the elements from the
original array.
The Array.slice()
method takes the following 2 parameters:
Here is an example of specifying both indexes in the call to the Array.slice()
method.
const arr = ['a', 'b', 'c', 'd']; const newArr = arr.slice(1, 3); console.log(newArr); // ๐๏ธ [ 'b', 'c' ] console.log(arr); // ๐๏ธ ['a', 'b', 'c', 'd']
We start extracting values at index 1
and go up to, but not including index
3
.
In other words, the new array contains the elements of the original array at
indices 1
and 2
.
This is a two-step process:
Call the splice()
method on the array, passing it the start index and the
number of elements to remove as arguments.
For example, arr.splice(0,2)
removes the first two elements from the array
and returns a new array containing the removed elements.
const arr = ['a', 'b', 'c', 'd']; const removeFirstTwo = arr.splice(0, 2); console.log(removeFirstTwo); // ๐๏ธ ['a', 'b'] console.log(arr); // ๐๏ธ ['c', 'd']
The code sample removes the first two elements from the array.
We passed the following 2 arguments to the Array.splice method:
0
.We supplied a start index of 0
and a delete count of 2
to remove the first 2
elements from the array.
const arr = ['a', 'b', 'c', 'd']; const removeFirstTwo = arr.splice(0, 2); console.log(removeFirstTwo); // ๐๏ธ ['a', 'b'] console.log(arr); // ๐๏ธ ['c', 'd']
If you need to remove the first 3 elements from the array, set the delete
count argument to 3
.
const arr = ['a', 'b', 'c', 'd']; const removeFirstThree = arr.splice(0, 3); console.log(removeFirstThree); // ๐๏ธ [ 'a', 'b', 'c' ] console.log(arr); // ๐๏ธ [ 'd' ]
The Array.splice()
method removes the elements from the original array and
returns them in a new array.
All of the elements from the start index onwards will be deleted, if:
const arr = ['a', 'b', 'c', 'd']; const removeAll = arr.splice(0); console.log(removeAll); // ๐๏ธ ['a', 'b', 'c', 'd'] console.log(arr); // ๐๏ธ []
We omitted the delete count parameter, which means that all elements from the specified start index onwards will get removed from the array.
Most experienced developers avoid mutating arrays and objects. Instead, they create a copy of the array containing only the elements that are needed.
Once you start mutating the same array multiple times, things get very confusing and difficult to debug.
You can learn more about the related topics by checking out the following tutorials: