Skip to content
the poor coder

combine 2 arrays javascript

Poor Man
Published
Reading time
2 min read
On this page

Combining 2 Arrays in JavaScript#

As a web developer, I have had to combine arrays in JavaScript numerous times. It is a common task when working with data, especially when you need to merge multiple sets of data into a single array. In this blog post, I will explain how you can combine two arrays in JavaScript.

Using the Concat Method#

The Concat method is the simplest way to combine two arrays in JavaScript. It creates a new array that contains all the elements from the original arrays. Here's an example:

Text

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

let combinedArr = arr1.concat(arr2);

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

The Concat method returns a new array that contains all the elements from arr1 followed by all the elements from arr2. It does not modify the original arrays.

Using the Spread Operator#

The Spread operator is another way to combine arrays in JavaScript. It allows you to spread the elements of one array into another array. Here's how you can use it:

Text

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

let combinedArr = [...arr1, ...arr2];

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

The Spread operator creates a new array that contains all the elements from arr1 followed by all the elements from arr2. It is a more concise way to combine arrays compared to the Concat method.

Using the Push Method#

The Push method is also a way to combine arrays in JavaScript. It adds the elements from one array to another array. Here's how you can use it:

Text

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

Array.prototype.push.apply(arr1, arr2);

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

The Push method modifies the original array (arr1) by adding the elements of another array (arr2) to it.

Conclusion#

There are multiple ways to combine arrays in JavaScript. The Concat method and Spread operator create a new array that contains all the elements from the original arrays, while the Push method modifies the original array by adding elements from another array to it. Choose the method that suits your needs and coding style.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC