The concat()
method combines multiple arrays into a single array. This can be useful for combining multiple arrays into a single array, or for creating a new array from existing arrays.
// create two arrays
const colors1 = ["red", "green", "blue"];
const colors2 = ["yellow", "orange", "pink"];
// combine the arrays
const colors = colors1.concat(colors2);
// print the combined array
console.log(colors); // Output: ["red", "green", "blue", "yellow", "orange", "pink"]
Here is another example of using the concat()
method to create a new array from existing arrays:
// create two arrays
const colors1 = ["red", "green", "blue"];
const colors2 = ["yellow", "orange", "pink"];
// combine the arrays
const colors = [].concat(colors1, colors2);
// print the combined array
console.log(colors); // Output: ["red", "green", "blue", "yellow", "orange", "pink"]
In this example, we create an empty array using the []
syntax and then use the concat()
method to combine the colors1
and colors2
arrays into the empty array. This creates a new array that contains all of the items from the colors1
and colors2
arrays.
Leave a Reply