Thursday, September 4, 2025
HomeLanguagesJavascriptAlternatives of push() method in JavaScript

Alternatives of push() method in JavaScript

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;">
        neveropen
    </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;">
        neveropen
    </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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS