How to Remove First Element of an Array in Javascript?

Sep 12, 2022 . Admin



Hello dev,

This example is focused on how to remove first element of an array in javascript. I would like to show you how to remove first element from array in javascript. This post will give you simple example of remove first item of the array in javascript. we will help you to give example of remove the first element from an array in javascript tutorial.

In this example, I am going to explain to you to remove the first element of an array in javascript.to remove first array element you have to use the shift() method. The shift() method removes the first element from an array and returns that removed element.

So, let's see bellow solution:

Example :
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>How to Remove First Element of an Array in Javascript? - MyWebtuts.com</title>
    </head>
    <body>
        <h1>How to Remove First Element of an Array in Javascript? -  MyWebtuts.com</h1>
        
        <script type="text/javascript">
            const colors = ["Red","Yellow","Green","White"];
            const firstElement = colors.shift();
            
            //Give the output of full array to console...
            console.log(colors);

            //Show removed element to console...
            console.log(firstElement);
        </script>
    </body>
</html>
Output:
['Yellow', 'Green', 'White']
Red
I hope it will help you...
#Javascript