Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Add new elements at the beginning of an array using JavaScript

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to push() method but it adds an element at the beginning of the array. 

Syntax:

ArrayObject.unshift( arrayElement );
  • ArrayObject: It is the array, where to insert the element.
  • arrayElement: It is the element, which is to be inserted.

Example: This example inserts element 6 at the beginning of the array GFG_Array

HTML




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>Add element using unshift()</h3>
    <p id="up"></p>
    <button onclick="myGFG()">
        Click to insert
    </button>
    <p id="down" style="color: green"></p>
  
    <!-- Script to add element at beginning of array -->
    <script>
        var GFG_Array = [1, 2, 3, 4, 5];
        var up = document.getElementById("up");
        up.innerHTML = GFG_Array;
        var down = document.getElementById("down");
        down.innerHTML = "elements of GFG_Array = "
                + GFG_Array;
                  
        function myGFG() {
            GFG_Array.unshift("6");
            down = document.getElementById("down");
            down.innerHTML = "element of GFG_Array = "
                    + GFG_Array;
        }
    </script>
</body>


Output:

 

Example: This example inserts the element GFG_7 at the beginning of the GFG_Array. 

HTML




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>Add element using unshift()</h3>
    <p id="up"></p>
    <button onclick="myGFG()">
        Click to insert
    </button>
    <p id="down" style="color: green"></p>
  
    <script>
        var GFG_Array = ["GFG_1", "GFG_2", "GFG_3", "GFG_4"];
        var up = document.getElementById("up");
        up.innerHTML = GFG_Array;
        var down = document.getElementById("down");
        down.innerHTML = "elements of GFG_Array = "
                + GFG_Array;
          
        function myGFG() {
            GFG_Array.unshift("GFG_7");
            down = document.getElementById("down");
            down.innerHTML = "element of GFG_Array = "
                    + GFG_Array;
        }
    </script>
</body>


Output:

 

Example:  In this example, we will use an array.splice method. The array.splice() method is used to modify the content of the array. 

HTML




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>JavaScript Insert at beginning using splice() method</h3>
    <p id="up"></p>
    <button onclick="myGFG()">
        Click to insert
    </button>
    <p id="down" style="color: green"></p>
  
    <!-- Script to add element at beginning of array -->
    <script>
        var GFG_Array = [1, 2, 3, 4, 5];
        var up = document.getElementById("up");
        up.innerHTML = GFG_Array;
        var down = document.getElementById("down");
        down.innerHTML = "elements of GFG_Array = "
                + GFG_Array;
                  
        function myGFG() {
            GFG_Array.splice(0, 0, "6");
            down = document.getElementById("down");
            down.innerHTML = "element of GFG_Array = "
                    + GFG_Array;
        }
    </script>
</body>


Output:

 


My Personal Notes arrow_drop_up
Last Updated : 20 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials