DEV Community

Nilesh Raut
Nilesh Raut

Posted on • Originally published at technilesh.com

4 Ways to Merge Arrays in JavaScript - SpeakLouder

Ways to Merge Arrays in JavaScript

JavaScript Arrays: A Quick Introduction

We've all been there – you're working on a JavaScript project, and you need to combine arrays to make your code more efficient. You might wonder: What are the best ways to merge arrays in JavaScript? Fear not, for today, we're going to explore various techniques to help you tackle this common programming challenge.

Check Our Blog for Detailed Explained : https://bit.ly/MergeArraysinJavaScript

1. Using the Concat() Method

One of the simplest ways to merge arrays is by using the concat() method. It allows us to combine multiple arrays effortlessly. Let's take a look at how it works:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const mergedArray = array1.concat(array2);

console.log(mergedArray); // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode
// hidden setup JavaScript code goes in this preamble area const array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const mergedArray = array1.concat(array2); // visible, reader-editable JavaScript code goes here console.log(mergedArray);

The concat() method returns a new array that contains elements from the arrays you want to merge. It doesn't modify the original arrays, making it a safe choice.

2. The Spread Operator (Three Dots)

If you prefer a more modern and concise approach, the spread operator can be your best friend. It allows us to spread the elements of an array into another array. Check it out:

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

console.log(mergedArray); // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode
const array1 = [1, 2, 3]; const array2 = [4, 5, 6]; const mergedArray = [...array1, ...array2]; console.log(mergedArray); // [1, 2, 3, 4, 5, 6]

The spread operator is not only a great way to merge arrays but also to copy, slice, or pass elements as function arguments. It's versatile and widely used in modern JavaScript.

3. Push and Spread – Combining Arrays

Sometimes, you might need to merge arrays by appending one to the end of another. For this, we can use the push() method along with the spread operator:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
array1.push(...array2);

console.log(array1); // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

This method is particularly useful when you want to modify the first array directly without creating a new one.

4. The Concatenate Method

If you prefer a functional programming approach, the concat() method has you covered here too. You can merge arrays using this method, chaining it for multiple arrays:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const array3 = [7, 8, 9];

const mergedArray = array1.concat(array2).concat(array3);

console.log(mergedArray); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode

This chaining approach can be especially helpful when you need to merge more than two arrays.

Conclusion

Merging arrays in JavaScript is a common task, and there are multiple ways to achieve it. Whether you prefer the classic concat() method, the modern spread operator, or a combination of methods, you now have the tools to tackle this challenge efficiently. Remember, the method you choose may depend on the specific requirements of your project, so pick the one that suits your needs best. Happy coding!

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

For number 4, chaining is a very inefficient use of concat, since it already allows you to pass as many arrays as you like. This is much better:

const array1 = [1, 2, 3]
const array2 = [4, 5, 6]
const array3 = [7, 8, 9]

const mergedArray = array1.concat(array2, array3)

console.log(mergedArray) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode