Q:

js add to array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors
79
array.push(element)
27
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']
2

var fruits = ["Banana", "Orange", "Apple", "Mango"];

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

fruits.push("Banana"); 
4
var fruits = ["222", "vvvv", "eee", "eeee"];

fruits.push("Kiwi"); 
11
let langages = ['Javascript', 'Ruby', 'Python']
langages.push('Go') // => ['Javascript', 'Ruby', 'Python', 'Go']

const dart = 'Dart'
langages = [...langages, dart] // => ['Javascript', 'Ruby', 'Python', 'Go', 'Dart']
0
const arr1 = [1,2,3]
const newValue = 4
const newData = [...arr1, obj] // [1,2,3,4]
4
//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
0

New to Communities?

Join the community