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

Related Articles

Alternatives of push() method in JavaScript

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

The task is to perform a push operation without using the push() method with the help of JavaScript. There are two approaches that are discussed below. 

Approach 1: Use the length property to insert the element at the end of the array.

Example: This example implements the above approach. 

html




<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h3>
        Alternatives of push() method in Javascript
    </h3>
    <p id="GFG_UP"></p>
    <button onclick="myGFG()">
        Click Here
    </button>
    <p id="GFG_DOWN"></p>
    <script>
        var arr = ["Element 1", "Element 2",
                    "Element 3", "Element 4"];
        var up = document.getElementById("GFG_UP");
        up.innerHTML = "Array = [" + arr + "]";
        var element = "Element x";
        var down = document.getElementById("GFG_DOWN");
        function myGFG() {
        arr[arr.length] = element;
        down.innerHTML = "Elements of array = [" + arr + "]";
                }
    </script>
</body>


Output: 

Alternatives of push() method in JavaScript

Alternatives of push() method in JavaScript

Approach 2 Use the [] notation to insert the element at the end of the array.

Example: This example implements the above approach. 

html




<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
    <h3>
        Alternatives of push() method in Javascript
    </h3>
    <p id="GFG_UP"></p>
    <button onclick="myGFG()">
        Click Here
    </button>
    <p id="GFG_DOWN"></p>
    <script>
        var arr = ["Element 1", "Element 2",
                    "Element 3", "Element 4"];
        var up = document.getElementById("GFG_UP");
        up.innerHTML = "Array = [" + arr + "]";
        var element = "Element x";
        var down = document.getElementById("GFG_DOWN");
        function myGFG() {
        arr = [arr, element];
        down.innerHTML = "Elements of array = [" + arr + "]";
                    }
    </script>
</body>


Output: 

Alternatives of push() method in JavaScript

Alternatives of push() method in JavaScript


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