The join()
method in TypeScript is used to join the elements of an array as a string.
array.join(separator)
separator
: This is a character or string that separates each element of the array when joined as a string.
This method returns the combination of all the elements of the array as a single string.
// 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"]// print elements of the arrays as a stringconsole.log(names.join(" ")) // separated by spaceconsole.log(numbers.join("+")) // separated by "+"console.log(cars.join("/")) // separated by "/"
names
, numbers
, and cars
and initialize them with elements.join()
method to combine the elements of names
using the separator, " "
, and print the returned string to the console.join()
method to combine the elements of numbers
using the separator, "+"
, and print the returned string to the console.join()
method to combine the elements of cars
using the separator, "/"
, and print the returned string to the console.