Sometimes we want to concatenate two existing array strings into one array in JavaScript.

Simply put, merge arrays.

At this point, we can use JavaScript’s concat function.

Look at the following code:

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
Copy the code

The above code merges the two arrays array1 and array2 into a new array called array3, in which the elements are added by Array2 after Array1.

If you need to merge more than two arrays, you can use the same method as above, but just pass in multiple arrays as arguments.

concat()
concat(value0)
concat(value0, value1)
concat(value0, value1, ... , valueN)
Copy the code

In the code above, you don’t need to run concat multiple times to merge. Concat is a method that allows you to pass multiple arrays to merge.

www.ossez.com/t/javascrip…