The push method in JavaScript is utilized to append one or several elements at the end of a specified array. By using this method, the original array's length is modified accordingly.
Syntax
The syntax for the push method is illustrated as follows:
Example
array.push(element1,element2....elementn)
Parameter
element1, element2, ..., elementn - The components that are to be included.
Return
The original array with added elements.
JavaScript Array push method example
Let's see some examples of push method
Example 1
Here, we will add an element in the given array.
Example
<script>
var arr=["AngularJS","Node.js"];
arr.push("JQuery");
document.writeln(arr);
</script>
Output:
Output
AngularJS,Node.js,JQuery
Example 2
Consider the following illustration to demonstrate how to insert multiple elements into an existing array.
Example
<script>
var arr=["AngularJS","Node.js"];
document.writeln("Length before invoking push(): "+arr.length+"<br>");
arr.push("JQuery","Bootstrap");
document.writeln("Length after invoking push(): "+arr.length+"<br>");
document.writeln("Update array: "+arr);
</script>
Output:
Output
Length before invoking push(): 2
Length after invoking push(): 4
Update array: AngularJS,Node.js,JQuery,Bootstrap