javascript add arrays code example

Example 1: javascript append to array

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

Example 2: merge array in js

//for ES5
var array1 = ["Rahul", "Sachin"];
var array2 = ["Sehwag", "Kohli"];

var output = array1.concat(array2);
console.log(output);

//for ES6, we use spread operators to concat arrays

var array1 = ["Dravid", "Tendulkar"];
var array2 = ["Virendra", "Virat"];

var output = [...array1, ...array2];
console.log(output);

Example 3: js add to array

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

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

console.log(arr);

Example 4: javascript append array to end of array

const new_array = old_array.concat([value1[, value2[, ...[, valueN]]]])

Example 5: how to add to an array js

var colors = ["blue", "red"];
    colors.push("black")

Tags:

Ruby Example