0
Q:

add an element to an 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
 // SPREAD OPERATOR
 const list1 = ["pepe", "luis", "rua"];
 const list2 = ["rojo", "verde", "azul"];
 const newList = [...list1, ...list2];
 // ["pepe", "luis", "rua", "rojo", "verde", "azul"]
2
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
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
7

New to Communities?

Join the community