.push() adds one or more new elements to the end of an array and returns the length of the newly expanded array.
var array = [1, 2, 3, 4, 5]; array.push(6);
.push accepts one or multiple parameters.
The length of the array is returned by
.push() ‘pushes’ new values onto the end (the right side) of an array.
Pushing a new element into an array
var array = [1, 2, 3, 4, 5]; var totalArrayElements = array.push(6); console.log(array); // Console will output 1, 2, 3, 4, 5, 6 console.log(totalArrayElements); // Console will output 6
push() method is used to add one or more new elements to the end of an array. It also returns the new length of the array.
arr.push([element1[, ...[, elementN]]])
- elementN The elements to add to the end of the array.
The new length of the array on which the method was called.
push() method will push elements to the end of an array. It can take zero or more arguments. If no arguments are provided, it will simply return the current length of the array. If provided one or more arguments, it will add these arguments to the array in the order in which they are written.
This method also returns the new length of the array after the element(s) are pushed to it.
var myStarkFamily = ['John', 'Robb', 'Sansa', 'Bran'];
Suppose you have an array of the children of House Stark from Game of Thrones. However, one of the members, Arya , is missing. Knowing the code above, you could add her by assigning
'Arya' to the array at the index after the last index like so:
myStarkFamily = 'Arya';
The problem with this solution is that it can’t handle general cases. If you didn’t know beforehand what the length of the array is, you can’t add new elements this way. This is what
push() is for. We don’t need to know how long the array is. We just add our element to the end of the array.
myStarkFamily.push('Arya'); console.log(myStarkFamily); // ['John', 'Robb', 'Sansa', 'Bran', 'Arya'] var newLength = myStarkFamily.push('Rickon'); // oops! forgot Rickon console.log(newLength); // 6 console.log(myStarkFamily); // ['John', 'Robb', 'Sansa', 'Bran', 'Arya', 'Rickon']