JavaScript(JS) array method - concat

https‮‬://www.theitroad.com

The concat() method is an array method in JavaScript that is used to merge two or more arrays into a single array. This method does not modify the original arrays, but instead returns a new array that contains the combined elements of the original arrays.

Here's the syntax of the concat() method:

let newArray = array1.concat(array2, array3, ..., arrayN);
  • array1 is the original array that the concat() method is called on.
  • array2, array3, ..., arrayN are the additional arrays to be merged with array1.
  • newArray is the new array that contains the combined elements of all the arrays.

Here's an example of how to use the concat() method:

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

let newArray = array1.concat(array2, array3);
console.log(newArray); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

In this example, we have three arrays (array1, array2, and array3) containing different elements. We use the concat() method to merge these arrays into a new array called newArray. The console.log() statement outputs the elements of the newArray.

Note that the concat() method can also be used with other data types, such as strings, numbers, and booleans. When using the concat() method with non-array values, the values are first converted to arrays and then merged with the original array.