Delete the first element of array without using shift() method in JavaScript

Given an array containing some array elements and the task is to remove the first element from the array and thus reduce the size by 1. We are going to perform shift() method operation without actually using it with the help of JavaScript.
There are two approaches that are discussed below:

Approach 1: We can use the splice() method that is used to get the part of the array.

  • Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Delete first element from the array without
            using shift() operation in JavaScript
        </title>
    </head>
      
    <body style="text-align:center;">
          
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
      
        <button onclick="myGFG()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
          
        <script>
            var arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
            var up = document.getElementById("GFG_UP");
            up.innerHTML = "Array - [" + arr + "]";
            var down = document.getElementById("GFG_DOWN");
            function myGFG() {
                arr.splice(0, 1);
                down.innerHTML = "elements of array = [" + arr + "]";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

Approach 2: We can use the filter() method to filter out the element at index 0.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Delete first element from the array without
            using shift() operation in JavaScript
        </title>
    </head>
      
    <body style="text-align:center;">
          
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
          
        <p id="GFG_UP"></p>
      
        <button onclick="myGFG()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
      
        <script>
            var arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
            var up = document.getElementById("GFG_UP");
            up.innerHTML = "Array - [" + arr + "]";
            var down = document.getElementById("GFG_DOWN");
            function removeFirst(element, index) {
                return index > 0;
            }
            function myGFG() {
                arr = arr.filter(removeFirst);
                down.innerHTML = "elements of array = [" 
                                + arr + "]";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.