merge two arrays in javascript

There are several ways to merge two arrays in JavaScript. One way is to use the concat() method, which joins two or more arrays and returns a new array. Another way is to use the spread operator ... to spread the elements of both arrays into a new array.

Here's an example of using the concat() method:

index.tsx
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let mergedArray = array1.concat(array2);
console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]
145 chars
5 lines

And here's an example of using the spread operator:

index.tsx
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let mergedArray = [...array1, ...array2];
console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 6]
146 chars
5 lines

Both methods produce the same output, which is a new array containing all the elements of both arrays.

gistlibby LogSnag