Below is the example of Array push() method.
- Example:
<script>functionfunc() {vararr = ['GFG','gfg','g4g'];// Pushing the element into the arrayarr.push('GeeksforGeeks');document.write(arr);}func();</script>chevron_rightfilter_none - Output:
GFG,gfg,g4g,GeeksforGeeks
The arr.push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array.
Syntax:
arr.push(element1, elements2 ....., elementN]])
Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.
Return value: This method returns the new length of the array after inserting the arguments into the array.
Below examples illustrate the JavaScript Array push() method:
- Example 1: In this example the function push() adds the numbers to the end of the array.
var arr = [34, 234, 567, 4]; print(arr.push(23,45,56)); print(arr);
Output:
7 34,234,567,4,23,45,56
- Example 2: In this example the function push() adds the objects to the end of the array.
var arr = [34, 234, 567, 4]; print(arr.push('jacob',true,23.45)); print(arr);Output:
7 34,234,567,4,jacob,true,23.45
More example codes for the above method are as follows:
Program 1:
<script> function func() { // Original array var arr = [34, 234, 567, 4]; // Pushing the elements document.write(arr.push(23,45,56)); document.write("<br>"); document.write(arr); } func(); </script> |
Output:
7 34,234,567,4,23,45,56
Program 2:
<script> function func() { var arr = [34, 234, 567, 4]; document.write(arr.push('jacob',true,23.45)); document.write("<br>"); document.write(arr); } func(); </script> |
Output:
7 34,234,567,4,jacob,true,23.45
Supported Browsers: The browsers supported by JavaScript Array push() method are listed below:
- Google Chrome 1.0
- Microsoft Edge 5.5
- Mozilla Firefox 1.0
- Safari
- Opera
Recommended Posts:
- How to push an array into the object in JavaScript ?
- Alternatives of push() method in JavaScript
- TypeScript | Array push() Method
- PHP | Ds\Sequence push() Function
- PHP | Ds\Vector push() Function
- PHP | Ds\Deque push() Function
- Sending Push Notifications : Progressive Web Apps
- PHP | SplDoublyLinkedList push() Function
- p5.js | Push Operation on Stack
- Node.js | push() function
- How to push a value based on matched value in PHP ?
- How to use .join() with push and replace together?
- Collect.js | push() Function
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to move an array element from one array position to another in JavaScript?
- How to get the elements of one array which are not present in another array using JavaScript?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to check whether an array is subset of another array using JavaScript ?
- How to convert Integer array to String array using JavaScript ?
- Difference between array.size() and array.length in JavaScript
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.


