What this method does is it merges two or more arrays. This method outputs the new merged array without altering the original arrays. Let’s show an example.
We have two arrays:
let arr1 = [4,2,1];
let arr2 = [1,2,1];
Then, we use Array.concat to merge the two arrays:
The output will be one large array concatenating arr2 to arr1:
[4,2,1,1,2,1] // expected output
Since we concatenated arr2 to arr1, the values in the arr2 array will come after the values in the arr1 array. If we wanted to flip the order, we will use the method to concatenate arr1 to arr2:
[1,2,1,4,2,1] // expected output. arr1 goes after arr2
Even though we merged arr1 and arr2 when we console log both of the arrays they are still intact.
And that is how Array.concat() works.
To see my other visual explanations on other methods of the Array class, check out these other recent articles: