merge two arrays in javascript es6 code example
Example 1: combine two arrays javascript
let arr1 = [0, 1, 2];
let arr2 = [3, 5, 7];
let primes = arr1.concat(arr2);
Example 2: javascript concat two arrays
const array3 = [...array1, ...array2];
Example 3: es6 concat array
let fruits = ["apples", "bananas"];
let vegetables = ["corn", "carrots"];
let produce = [...fruits, ...vegetables];
Example 4: concatenate multiple arrays javascript
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = [...array1, ...array2];
console.log(array3);
Example 5: javascript merge two array with spread operator
const a = ['to', 'code'];
const b = ['learning', ...a, 'is', 'fun'];
console.log(b);
Example 6: combine 2 arrays javascript
const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ];
const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ]
const allItems = [ ...itemsA, ...itemsB ];