LukeH
0
Q:

add element to array javascript

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
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
7

New to Communities?

Join the community