add a new object to an array javascript code example
Example 1: javascript push array
const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
console.log(count);
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
Example 2: how to append objects to javascript lists ?
var studentList = ['Jason', 'Samantha', 'Alice', 'Joseph'];
//Add a new student to the end of the student list
studentList.push('Jacob');
//list is updated to ['Jason', 'Samantha', 'Alice', 'Joseph', 'Jacob'];