TypeScript Array concat() Method
The Array.concat() is an inbuilt TypeScript function that is used to merge two or more arrays.
Syntax:
array.concat(value1, value2, ..., valueN)
Parameter: This method accepts a single parameter multiple times as mentioned above and described below:
- valueN: These parameters are arrays and/or values to concatenate.
Return Value: This method returns the new array.
Examples of TypeScript Array concat() Method
The below examples illustrate the Array concat() method in TypeScript.
Example 1: The TypeScript code defines three arrays, then concatenates them using the concat() method and logs the result to the console.
// TypeScript code
let num1: number[] = [11, 12, 13];
let num2: number[] = [14, 15, 16];
let num3: number[] = [17, 18, 19];
// Using the concat() method in TypeScript
console.log(num1.concat(num2, num3));
Output:
[ 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
Example 2: In TypeScript, define two arrays of strings, then merge them using the concat method.
// TypeScript code
let str1: string[] = ['a', 'b', 'c'];
let str2: string[] = ['d', 'e', 'f'];
// Using the concat() method in TypeScript
console.log(str1.concat(str2));
Output:
[ 'a', 'b', 'c', 'd', 'e', 'f' ]
Contact Us