In JavaScript we can use the Array Object which has the facility to store multiple values into a single variable.
JavaScript array push
In JavaScript we can use the Array Object which has the facility to store multiple values into a single variable. For manipulating array objects and controlling their values we have some methods such as push(), pop(), reverse() etc. Here in this example code we have described about the JavaScript's array object push() method.
We can add the elements into the array object with the use of push() method. push() method adds one or more elements to the array object and also returns the new length of the array.
In our example of JavaScript array push we have created a JavaScript array object "arr" and it contains two elements "Rose" and "India" and we have added one more element into it by using the method push(). Here is the full example code and its output of our example :
<html> <head> <title>JavaScript array push example</title> <script type="text/javascript"> var arr=new Array(2); arr[0]="Rose"; arr[1]="India"; document.write("Array arr contains(Before push())="+arr+"</br>"); arr.push("Technologies"); document.write("Array arr contains(After push()) ="+arr+"</br>"); </script> </head> <body> <h2>Example of array's push() </h2> Push method insert the new element at the end of array </body> </html> |