The push() method adds one or more elements to the end of an array and returns the new length of the array.
Syntax
arr.push(element1[, ...[, elementN]])
elementN - The elements to add to the end of the array.
Example 1: Simple Array.push() method example
var animals = ['pigs', 'goats', 'sheep'];
console.log(animals.push('cows'));
console.log(animals);
animals.push('chickens');
console.log(animals);
Output:
4
["pigs", "goats", "sheep", "cows"]
["pigs", "goats", "sheep", "cows", "chickens"]
Example 2: Adding elements to an array
var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming');
console.log(sports);
console.log(total);
Output:
["soccer", "baseball", "football", "swimming"]
4
Comments
Post a Comment
Leave Comment