The Array.toString() method in TypeScript is a built-in function used to convert an array and its elements into a string representation. This method is useful when you need a simple, comma-separated string version of your array.
Syntax
The syntax for using the toString() method is straightforward:
array.toString()Parameter: This method does not accept any parameter.
Return Value: This method returns the string representing the array.
Examples of TypeScript Array toString() Method
Example 1: Converting a Number Array to a String
In this example, we will convert an array of numbers into a string.
let numbers: number[] = [11, 89, 23, 7, 98];
let result: string = numbers.toString();
console.log(result); // Output: "11,89,23,7,98"
Output:
11,89,23,7,98Example 2: Converting a String Array to a String
In this example, we will convert an array of characters into a string.
let characters: string[] = ["G", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"];
let result: string = characters.toString();
console.log(result);
Output:
G,e,e,k,s,f,o,r,g,e,e,k,s