How to concatenate array in typescript

An array is a data structure that represents a list of values. Arrays are used to store data linearly and can be used to store data of any type.

This post will show three ways to merge an Array using TypeScript.

Use the Array.concat() method:

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

let arr3 = arr1.concat(arr2);

console.log(arr3); // [1, 2, 3, 4, 5, 6]

Use the spread operator (...) to expand the arrays into individual values:

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

let arr3 = [...arr1, ...arr2];

console.log(arr3); // [1, 2, 3, 4, 5, 6]

Use the Array.push() method to push all the values of one array into another:

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

arr1.push(...arr2);

console.log(arr1); // [1, 2, 3, 4, 5, 6]

Feel free to reach out if you have any questions or comments.