Push In Array JavaScript

Push In Array JavaScript | When working with an ordered list of values, the array datatype is one of the most often used. With a distinct id, each value is referred to as an element. You can access several data types stored there through a single variable.

An array might keep a list of users, and we might need to add an element or element to the array at any particular point in the array, such as just after the last element or before the first element. This article will demonstrate how to use JavaScript to push an element to the end of an array.

To add a value or values to the end of an array in JavaScript, use the push() method. The array’s length is adjusted using this technique to reflect the number of new elements added.

Syntax:-
arr.push(element1, elements2 ….., elementN] ] )

The number of parameters in this method equals the number of elements that will be added to the array.

Return value: Following the insertion of the arguments into the array, this method returns the array’s new length.

Example 1:- An instance of the Array push() method is given below.

var arr = ["Sun", "Mon", "Tue"];
// pushing the element into the array
arr.push("Wed");
console.log(arr);

Output:-

[ ‘Sun’, ‘Mon’, ‘Tue’, ‘Wed’ ]

Example 2:- The numbers are added to the end of the array in this example using the push() function.

// Original array
var arr = [87, 4548, 698, 3];

// Pushing the elements
console.log(arr.push(90, 16, 64, 434));
console.log(arr);

Output:-

8
[
87, 4548, 698,
3, 90, 16,
64, 434
]

Example 3:- The items are added to the end of the array using the push() function in this example.

// Original array
var arr = [87, 4548, 698, 3, 90, 16];

// Pushing the elements
console.log(arr.push("Jack", 87.344, true));
console.log(arr);

Output:-

9
[
87, 4548, 698,
3, 90, 16,
‘Jack’, 87.344, true
]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *