JavaScript | Array push()

arr.push() function is used to push one or more values into the array. This function changes the length of the array by the number of elements added to the array. The syntax of the function is as follows:

arr.push(element1[, ...[, elementN]])

Arguments This function contains as many numbers of arguments as the number of elements to be inserted into the array.

Return value This function returns the new length of the array after inserting the arguments into the array.



Examples for the above function are provided below:

Example 1:

var arr = [34, 234, 567, 4];
print(arr.push(23,45,56));
print(arr);

Output:

7
34,234,567,4,23,45,56

In this example the function push() adds the numbers to the end of the array.

Example 2:

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

In this example the function push() adds the objects to the end of the array.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate push() function
<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>

chevron_right


Output:

7
34,234,567,4,23,45,56

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate push() function
function func() {
  
    var arr = [34, 234, 567, 4];
  
    document.write(arr.push('jacob',true,23.45));
    document.write("<br>");
    document.write(arr);
}
func();
</script>

chevron_right


Output:

7
34,234,567,4,jacob,true,23.45


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.