concatenate two arrays in javascript

To concatenate two arrays in JavaScript, you can use the concat() method. This method joins two or more arrays and returns a new array that contains all the elements from the original arrays.

Here is an example:

index.tsx
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const concatenatedArray = array1.concat(array2);
console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6]
163 chars
5 lines

In this example, we have two arrays array1 and array2. We then use the concat() method to join them into a new array called concatenatedArray. Finally, we print the result in the console.

You can also concatenate more than two arrays by passing them as arguments to the concat() method:

index.tsx
const array1 = [1];
const array2 = [2];
const array3 = [3];
const array4 = [4];
const concatenatedArray = array1.concat(array2, array3, array4);
console.log(concatenatedArray); // Output: [1, 2, 3, 4]
201 chars
7 lines

In this example, we have 4 separate arrays being concatenated together.

gistlibby LogSnag