push value in array code example
Example 1: javascript array push
let animals = ["dog","cat","tiger"];
animals.pop(); // ["dog","cat"]
animals.push("elephant"); // ["dog","cat","elephant"]
Example 2: js add to array
const myArray = ['hello', 'world'];
// add an element to the end of the array
myArray.push('foo'); // ['hello', 'world', 'foo']
// add an element to the front of the array
myArray.unshift('bar'); // ['bar', 'hello', 'world', 'foo']
// add an element at an index of your choice
// the first value is the index you want to add at
// the second value is how many you want to delete (0 in this case)
// the third value is the value you want to insert
myArray.splice(2, 0, 'there'); // ['bar', 'hello', 'there', 'world', 'foo']
Example 3: how to push items in array in javascript
let items = [1, 2, 3]
items.push(4); // items = [1, 2, 3, 4]
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"]