Best Solution: Using Array.concat()
The Array.concat()
method in JavaScript is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
Here’s an example of how to use concat()
to join two arrays:
// Two arrays
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
// Join the arrays
let joinedArray = array1.concat(array2);
console.log(joinedArray);
// Output: [1, 2, 3, 4, 5, 6]
In the code above, concat()
merges array1
and array2
into a new array joinedArray
.
Using the Spread Operator (…)
The spread operator (...
) in JavaScript is a modern ES6 feature that provides a concise way to work with arrays and objects. When used with arrays, it can be particularly useful for merging or joining two or more arrays together.
Here’s how you can use the spread operator to join arrays:
// Two arrays
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
// Use the spread operator to join the arrays
let joinedArray = [...array1, ...array2];
console.log(joinedArray);
// Output: [1, 2, 3, 4, 5, 6]
In this example, ...array1
spreads out the elements of array1
, and ...array2
does the same for array2
. The result is a new array that contains all the elements from both array1
and array2
.
One of the key benefits of the spread operator is that it creates a new array rather than modifying any of the original arrays. This means you can safely use it without worrying about unintended side effects. Also, it’s worth mentioning that the spread operator can be used not just with arrays, but with any iterable object.
Using push() with apply()
The push()
method in JavaScript is used to add one or more elements to the end of an array, and returns the new length of the array. However, when you want to merge arrays, you can’t directly use push()
, because it will add the whole second array as a single element at the end of the first array.
Here’s what happens if you try to use push()
directly:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
array1.push(array2);
console.log(array1);
// Output: [1, 2, 3, [4, 5, 6]]
As you can see, array2
was added as a sub-array, not as individual elements. To avoid this, you can use the apply()
method in combination with push()
.
The apply()
method calls a function with a given this
value, and arguments
provided as an array (or an array-like object). When used with push()
, it essentially pushes the elements of the second array one by one into the first array.
Here’s how you can use push()
and apply()
to merge arrays:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
Array.prototype.push.apply(array1, array2);
console.log(array1);
// Output: [1, 2, 3, 4, 5, 6]
In this code, Array.prototype.push.apply(array1, array2);
applies the push()
method to array1
, using the elements of array2
as arguments. This effectively pushes each element of array2
into array1
, achieving the desired result.
It’s important to note that this method modifies the original array (array1
), so if you want to keep the original arrays intact, you should use a different method like concat()
or the spread operator.
Using Array.forEach()
For the sake of completeness, I’m including the forEach()
option as well. As with a lot of tasks, you can use a simple loop to merge or join arrays. But this method is not efficient nor clean, so I don’t recommend it.
Here’s how you can use forEach()
to merge arrays:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
// Use forEach() to push each item from array2 into array1
array2.forEach(item => array1.push(item));
console.log(array1);
// Output: [1, 2, 3, 4, 5, 6]
In this code, array2.forEach(item => array1.push(item));
uses the forEach()
method to loop through each element (item
) in array2
. For each element, it calls the push()
method to add that element to the end of array1
.