Answers for "how to add a new item in an array in javascript"

187

javascript add to array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors
Posted by: Guest on July-22-2019
1

how to add a new item in an array in javascript

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")
Posted by: Guest on March-21-2021
6

javascript adding an array to an array

let chocholates = ['Mars', 'BarOne', 'Tex'];
let chips = ['Doritos', 'Lays', 'Simba'];
let sweets = ['JellyTots'];

let snacks = [];
snacks.concat(chocholates);
// snacks will now be ['Mars', 'BarOne', 'Tex']
// Similarly if we left the snacks array empty and used the following
snacks.concat(chocolates, chips, sweets);
//This would return the snacks array with all other arrays combined together
// ['Mars', 'BarOne', 'Tex', 'Doritos', 'Lays', 'Simba', 'JellyTots']
Posted by: Guest on March-04-2020
5

add item to array javascript

const arr1 = [1,2,3]
const newValue = 4
const newData = [...arr1, obj] // [1,2,3,4]
Posted by: Guest on April-17-2020

Code answers related to "how to add a new item in an array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language