add in array javascript code example

Example 1: javascript append to array

var colors=["red","white"];
colors.push("blue");//append 'blue' to colors

Example 2: js add item to array

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

Example 3: javscript append item from array

let foo = ['oop','plop','copo'];
	foo.push("plop");

Example 4: pushing element in array in javascript

array = ["hello"]
array.push("world");

Example 5: js add to array

let arr = [1, 2, 3, 4];

arr = [...arr, 5, 6, 7];

console.log(arr);

Example 6: js insert in array

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

Tags:

Misc Example