JavaScript Array concat() method

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: