Visual Explanation: JavaScript Array.concat()

Erica N
Erica N
Dec 5, 2019 · 2 min read

Today we are going to learn about another JavaScript method under the Array class called .concat().

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:

arr1.concat(arr2);

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:

arr2.concat(arr1);
[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.


JavaScript in Plain English

Learn the web's most important programming language.

Erica N

Written by

Erica N

An artist that uses illustrations to solve easy programming algorithms.

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade