Array in JavaScript push() Method appends the given element or elements in the last of array. It returns the length of the new array.
Syntax is as below:
1 | array.push(item1, item2,.....itemN); |
item1, item2, …..itemN: Items to add at the end of array
Return Value:
push() method returns the length of the new array.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <title>push() Method</title> </head> <body> <script type="text/javascript"> var myArr = ["jQuery", "PHP", "HTML", "CSS", "WordPress"]; document.write("Array: " + myArr); var length = myArr.push("MySQL"); document.write("<br /><br />New array is : " + myArr); document.write("<br />New length : " + length); length = myArr.push("Drupal", "JavaScript"); document.write("<br /><br />New array is : " + myArr); document.write("<br />New length : " + length); </script> </body> </html> |
Output: This will give the following output:
1 2 3 4 5 6 7 | Array: jQuery,PHP,HTML,CSS,WordPress New array is : jQuery,PHP,HTML,CSS,WordPress,MySQL New length : 6 New array is : jQuery,PHP,HTML,CSS,WordPress,MySQL,Drupal,JavaScript New length : 8 |
Tip: To add items at the beginning of an array, use the unshift() method.