Remove First Item From Array JavaScript
If you use the code arr[0] = ' '
to remove the first element in the list of elements in the array, the first item in your result will be an empty string ['', 2, 3, 4, 5].
So you have to remove the first item and shift the other items to one step back. We can remove the first element and shifts with inbuilt methods like shift, and slice, and without inbuilt methods like if condition and for a loop. In this article, I will tell you how to remove the first item from an array in Javascript
To remove the first element without using the in-built methods, we use a for loop to set the condition “i < array.length - 1
“, which ensures that the loop loops over all elements of the array except the last one. Inside the loop, shifts each element is one position to the left, For example, in the first iteration of the loop, array[0]
will be assigned the value of array[1]
, effectively shifting the elements toward the beginning of the array. Finally, we decreased the length of the array by 1, which removes the first element of the array.
Example:
function removeFirstItemFromArray(array) {
if (array.length === 0) {
return array; // Array is already empty
}
for (let i = 0; i < array.length - 1; i++) {
array[i] = array[i + 1]; // Shift each element one position to the left
}
array.length--; // Decrease the length of the array
return array;
}
// Example usage:
const array = [1, 2, 3, 4, 5];
removeFirstItemFromArray(array);
console.log(array); // Output: [2, 3, 4, 5]
You may find it difficult to use this method, so let’s see how to remove the first element in the array using the built-in methods.
2 Ways to remove the first item from an array
1. Shift() method
One of the simplest ways to remove the first item from an array is by using the shift()
method. You can remove the first item and shift all remaining elements to a lower index by just invoking the shift() method on your array. This method returns the removed element and updates the length property of the array.
const array = [1, 2, 3, 4, 5];
array.shift();
console.log(array); // Output: [2, 3, 4, 5]
While shift() is convenient, it is worth noting that it can be inefficient for large arrays. Since it modifies the entire array by shifting all elements, it has a time complexity of O(n), where n represents the number of elements in the array.
2. Slice() method
Another approach to removing the first item from an array is by using the slice() method. The slice() method returns a new array containing a shallow copy of a portion of the original array. By specifying a starting index of 1, we can obtain a new array without the first element.
const array = [1, 2, 3, 4, 5];
const newArray = array.slice(1);
console.log(newArray); // Output: [2, 3, 4, 5]
It allows us to avoid modifying the original array, making it a safer option. This method also has a time complexity of O(n), as it needs to create a new array and copy the elements from the original array.
Conclusion
So if you want to remove the first element in a single line of code use shift() or slice() build-in methods.