how to push into an array code example

Example 1: js array add element

array.push(element)

Example 2: javascript pushing to an array

some_array = ["John", "Sally"];
some_array.push("Mike");

console.log(some_array); //output will be =>
["John", "Sally", "Mike"]

Example 3: add item to list javascript

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi"); // Fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];

Example 4: how to push array

//the array comes here
var numbers = [1, 2, 3, 4];

//here you add another number
numbers.push(5);

//or if you want to do it with words
var words = ["one", "two", "three", "four"];

//then you add a word
words.push("five")

//thanks for reading

Example 5: how to push values in 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 6: 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'];

Tags:

Misc Example