How to useArray.concat() Method in Javascript
This approach concatenates the source array with an empty array using the Array.prototype.concat() method, which returns a new array with all the elements.
Syntax:
const newArray = [].concat(sourceArray);
Example: In this example, we will see the use of Array.concat() Method.
const sourceArray = [1, 2, 3, 4, 5];
const newArray = [].concat(sourceArray);
console.log(newArray);
Output
[ 1, 2, 3, 4, 5 ]
Copy Array Items into Another Array in JavaScript
We will see how to copy array items into another array in JavaScript. This operation is valuable when you need to generate a new array containing either identical or a subset of elements from an original array.
These are the following ways to solve this problem:
Table of Content
- Using the Spread Operator (…)
- Using slice() Method
- Using apply() Method
- Using Array.concat() Method
- Using push() Method
- Using map() Method
Contact Us