The toString() method returns a string representing the specified array and its elements.
The syntax of Array.toString() method is:
toString(): string
This toString() method of Array returns a string representation of an array.
Array.toString() returns value of type string
.
The toString() method can be used to convert a numeric array to a string.
For example,
arr
with numeric values [1, 2, 3, 4, 5].toString()
to convert the array to a string.result
.result
to the console.const arr = [1, 2, 3, 4, 5];
const result = arr.toString();
console.log(result);
1,2,3,4,5
The toString() method can be used to convert an array of strings to a single string.
For example,
arr
with string values ['a', 'b', 'c'].toString()
to convert the array to a string.result
.result
to the console.const arr = ['a', 'b', 'c'];
const result = arr.toString();
console.log(result);
a,b,c
The toString() method can be used to convert an array with mixed types to a string.
For example,
arr
with mixed values [1, 'a', true, null].toString()
to convert the array to a string.result
.result
to the console.const arr = [1, 'a', true, null];
const result = arr.toString();
console.log(result);
1,a,true,
In this TypeScript tutorial, we learned about toString() method of Array: the syntax and few working examples with output and detailed explanation for each example.