The concat() method in TypeScript is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array containing the values of the joined arrays.
There are 2 variations for the syntax of Array.concat() method. They are:
concat(...items: ConcatArray<T>[]): T[]
Parameters
Parameter | Optional/Required | Description |
---|---|---|
items | optional | Arrays and/or values to concatenate into a new array. |
This method combines multiple arrays or values into a new array.
Returns value of type Array
.
concat(...items: (T | ConcatArray<T>)[]): T[]
Parameters
Parameter | Optional/Required | Description |
---|---|---|
items | optional | Arrays and/or values to concatenate into a new array. |
This method combines multiple arrays or values into a new array.
Returns value of type Array
.
In TypeScript, you can use the concat()
method to merge multiple arrays into a new array.
For example,
arr1
and arr2
with some initial values.concat()
method to merge arr1
and arr2
into a new array.mergedArray
.mergedArray
to the console using the console.log()
method.const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const mergedArray = arr1.concat(arr2);
console.log(mergedArray);
[1, 2, 3, 4, 5, 6]
In TypeScript, you can use the concat()
method to concatenate multiple values to an array.
For example,
arr
with some initial values.concat()
method to concatenate additional values to arr
.newArray
.newArray
to the console using the console.log()
method.const arr = [1, 2, 3];
const newArray = arr.concat(4, 5, 6);
console.log(newArray);
[1, 2, 3, 4, 5, 6]
In TypeScript, you can use the concat()
method to concatenate arrays and values together.
For example,
arr1
and arr2
with some initial values.concat()
method to concatenate arr1
, arr2
, and additional values together.resultArray
.resultArray
to the console using the console.log()
method.const arr1 = [1, 2];
const arr2 = [5, 6];
const resultArray = arr1.concat(3, 4, arr2);
console.log(resultArray);
[1, 2, 3, 4, 5, 6]
In this TypeScript tutorial, we learned about concat() method of Array: the syntax and few working examples with output and detailed explanation for each example.