In TypeScript, we can join two or more arrays using the concat()
method. We can join two, three, or more arrays using this method.
array1.concat(array2, array3, ... arrayN)
array1
, array2
, array3
,
and arrayN
: These are the arrays that we want to join together.
The concat()
method returns a new array with all the elements of the joined arrays.
// create arrays in TypeScriptlet names: string[] = ["Theodore", "James", "Peter", "Amaka"]let developers: string[] = ["Jane", "Onyejiaku"]let numbers: Array<number>;numbers = [12, 34, 5, 0.9]let evenNumbers: Array<number> = [2, 4, 6]let oddNumbers: Array<number> = [1, 3, 5, 7]let cars : Array<string> = ["Porsche", "Toyota", "Lexus"]// join all arrayslet allNumbers = numbers.concat(evenNumbers, oddNumbers)let allStrings = developers.concat(cars)// print concatenated arraysconsole.log(allNumbers)console.log(allStrings)
concat()
method.