push items into array javascript code example

Example 1: js add item to array

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

Example 2: push array javascript

let array = ["A", "B"];
let variable = "what you want to add";

//Add the variable to the end of the array
array.push(variable);

//===========================
console.log(array);
//output =>
//["A", "B", "what you want to add"]

Example 3: javascript add items to array

//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

Example 4: how to push items in array in javascript

let items = [1, 2, 3]
items.push(4); // items = [1, 2, 3, 4]

Example 5: javascript add element to array

const langages = ['Javascript', 'Ruby', 'Python'];
langages.push('Go'); // => ['Javascript', 'Ruby', 'Python', 'Go']

const dart = 'Dart';
langages = [...langages, dart]; // => ['Javascript', 'Ruby', 'Python', 'Go', 'Dart']

Example 6: add item to array javascript

const arr1 = [1,2,3]
const newValue = 4
const newData = [...arr1, obj] // [1,2,3,4]