let dailyActivities = ['eat', 'sleep'];
// add an element at the end
dailyActivities.push('exercise');
console.log(dailyActivities); // ['eat', 'sleep', 'exercise']
//adding items to array
objects = [];
objects.push("you can add a string,number,boolean,etc");
//if you more stuff in a array
//before i made a error doing value = : value
objects.push({variable1 : value, variable2 : value);
//we do the {} so we tell it it will have more stuff and the variable : value
// initialize array
var arr = [
"Hi",
"Hello",
"Bonjour"
];
// append new value to the array
arr.push("Hola");
console.log(arr);
Run code snippetHide results
how to append an element to an array in javascript
//Use push() method
//Syntax:
array_name.push(element);
//Example:
let fruits = ["Mango", "Apple"];
//We want to append "Orange" to the array so we will use push() method
fruits.push("Orange");
//There we go, we have successfully appended "Orange" to fruits array!
var arrayExample = [53,'Hello World!'];
console.log(arrayExample) //Output =>
[53,'Hello World!']
//You can also do this
arrayExample.push(true);
console.log(arrayExample); //Output =>
[53,'Hello World!',true];
let array = ["Chicago", "Los Angeles", "Calgary", "Seattle", ]
// print the array
console.log(array)
//Adding a new item in an array without touching the actual array
array.push('New Item') < variable_name > .push( < What you want to add > )
//How many items does the array have?
console.log("This array has", array.length, "things in it")