How do you concatenate two arrays in JavaScript?

In JavaScript, you can concatenate two arrays using various built-in methods and techniques. In this article, I will explain some of the most common ways to concatenate two arrays using code snippets.

Method 1: Using the concat() Method:

The easiest and most straightforward way to concatenate two arrays in JavaScript is by using the concat() method. This method creates a new array that includes all the elements from the original arrays.

Here’s an example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];

const newArray = array1.concat(array2);

console.log(newArray); // Output: [1, 2, 3, 4, 5, 6]

In this example, we declared two arrays array1 and array2 that we want to concatenate. We then used the concat() method to create a new array newArray that includes all the elements from both arrays.

Method 2: Using the Spread Operator:

Another way to concatenate two arrays is by using the spread operator (...). This operator is used to spread the elements of an array or an iterable object, such as a string or a set.

Here’s an example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];

const newArray = [...array1, ...array2];

console.log(newArray); // Output: [1, 2, 3, 4, 5, 6]

In this example, we declared two arrays array1 and array2 that we want to concatenate. We then used the spread operator to spread the elements of both arrays into a new array newArray.

Method 3: Using the push() Method:

The push() method can also be used to concatenate two arrays. This method adds one or more elements to the end of an array and returns the new length of the array.

For example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];

array1.push(...array2);

console.log(array1); // Output: [1, 2, 3, 4, 5, 6]

In this example, we declared two arrays array1 and array2 that we want to concatenate. We then used the spread operator to spread the elements of array2 into the push() method of array1.

Method 4: Using the splice() Method:

The splice() method can also be used to concatenate two arrays. This method changes the content of an array by removing existing elements and/or adding new elements.

Here’s an example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];

array1.splice(array1.length, 0, ...array2);

console.log(array1); // Output: [1, 2, 3, 4, 5, 6]

In this example, we declared two arrays array1 and array2 that we want to concatenate. We then used the splice() method to insert the elements of array2 into array1 at the end of the array.

Method 5: Using a for Loop:

Finally, you can concatenate two arrays using a for loop. This method involves iterating through the elements of the second array and adding them one by one to the end of the first array.

Here’s an example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];

for (let i = 0; i < array2.length; i++) {
  array1.push(array2[i]);
}

console.log(array1); // Output: [1, 2, 3, 4, 5, 6]

In this example, we declared two arrays array1 and array2 that we want to concatenate. We then used a for loop to iterate through the elements of array2 and add them one by one to the end of array1 using the push() method.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: