Alternatives of push() method in JavaScript - GeeksforGeeks

02 Jun, 2020
The task is to perform 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.<!DOCTYPE html><html>    <head>        <title>            Alternatives of push() method 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 = ["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></html>
<!DOCTYPE html><html>    <head>        <title>            Alternatives of push() method 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 = ["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></html>
Output:
Approach 2 Use the [] notation to insert the element at the end of array.
Example: This example implements the above approach.<!DOCTYPE html><html>    <head>        <title>            Alternatives of push() method 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 = ["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></html>
<!DOCTYPE html><html>    <head>        <title>            Alternatives of push() method 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 = ["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></html>
Output:
javascript-functions
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Difference Between PUT and PATCH Request
Node.js | fs.writeFileSync() Method
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?