The toString()
method of an array in TypeScript is used to return the string representation of all the elements of the array.
array.toString()
array
: This is the array we want to get the string representation of.
This method returns a string containing the combined elements of the array.
// create arrays in TypeScriptlet names: string[] = ["Theodore", "James", "Peter", "Amaka"]let numbers : Array<number>;numbers = [12, 34, 5, 0.9]let cars : Array<string> = ["Porsche", "Toyota", "Lexus"]let randomValues : Array<string | number> = ["one",1, "two", 2, 56]// get the arrays as stringslet str1 = names.toString()let str2 = numbers.toString()let str3 = cars.toString()let str4 = randomValues.toString()// print the arrays as stringsconsole.log("String representation:",str1, "| Data type:",typeof(str1))console.log("String representation:",str2, "| Data type:",typeof(str2))console.log("String representation:",str3, "| Data type:",typeof(str3))console.log("String representation:",str4, "| Data type:",typeof(str4))
toString()
method.