The concat()
method creates a new array by merging (concatenating) existing arrays:
Example (Merging Two Arrays)
const myGirls = ["Cecilie", "Lone"];
const myBoys = ["Emil", "Tobias", "Linus"];
// Concatenate (join) myGirls and myBoys
const myChildren = myGirls.concat(myBoys);
const myBoys = ["Emil", "Tobias", "Linus"];
// Concatenate (join) myGirls and myBoys
const myChildren = myGirls.concat(myBoys);
The concat()
method does not change the existing arrays. It always returns a new array.
The concat()
method can take any number of array arguments:
Example (Merging Three Arrays)
const arr1 = ["Cecilie", "Lone"];
const arr2 = ["Emil", "Tobias", "Linus"];
const arr3 = ["Robin", "Morgan"];
const myChildren = arr1.concat(arr2, arr3);
const arr2 = ["Emil", "Tobias", "Linus"];
const arr3 = ["Robin", "Morgan"];
const myChildren = arr1.concat(arr2, arr3);
The concat()
method can also take strings as arguments:
Example (Merging an Array with Values)
const arr1 = ["Emil", "Tobias", "Linus"];
const myChildren = arr1.concat("Peter");
const myChildren = arr1.concat("Peter");
Practice Excercise Practice now