How to Remove First Element From Array in Javascript

Last Updated on April 17, 2024 by Roshan Parihar

To remove the first element from the array in JavaScript, use the shift() function without the need to pass any arguments.

You can also use the splice() function and pass the argument to the index position as zero(0) to delete the first element.

Let’s see some examples given below.

Using shift() Function in Javascript to Remove First Element From Array

If you want to remove the first element from an array, you can use the shift() function. It does not require any argument to pass for deletion. See the examples to learn how to remove the first element.

Examples 1

Output


The above example contains the first element as ‘9’. You can click the above button to remove the first element. The output shows that the element is removed.

With splice() Function to Delete Initial Item

If you want to delete the first item from an array, use the splice() function of javascript. It takes two arguments in comma separation in which the first argument is the index position of the first item which is zero (0). The second argument is the number of elements to remove from the array which is 1 to delete.

Examples 2

Output


When you click the button given above, the first element gets deleted. This function is also useful to delete the specified element using its index position.

You May Also Like to Read

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.