The join()
method is an array method in Google Apps Script that joins all the elements of an array into a string. It takes an optional argument that specifies the separator between each element in the resulting string. By default, the separator is a comma (,
), but you can use any character or string as the separator.
For example, let’s say we have an array of country names:
var countries = ["USA", "Canada", "Mexico", "Brazil", "Argentina"];
We can use the join()
method to join all the elements of the array into a single string, separated by commas:
var countryList = countries.join();
In this example, we call the join()
method on the countries
array and assign the resulting string to a variable called countryList
. The value of countryList
will be the string "USA,Canada,Mexico,Brazil,Argentina"
.
We can pass it as an argument to the method if we want to use a different separator. For instance, if we want to separate the country names with a hyphen (-
), we can do the following:
var countryList = countries.join("-");