concat() method

The concat() method in Apps Script is used to concatenate two or more strings together, and returns a new string that is the result of joining the strings.

Example 1: Concatenating Two Strings
Suppose we have two strings country and city containing the name of a country and a city, respectively, and we want to concatenate them together into a single string. We can use the concat() method like so:

var country = "Germany";
var city = "Berlin";
var fullLocation = country.concat(" - ", city);
Logger.log(fullLocation);

In this example, we define two strings country and city containing the name of a country and a city, respectively, and use the concat() method to join them together into a new string separated by a dash. The resulting string “Germany – Berlin” is logged to the console.

Example 2: Concatenating Multiple Strings
Suppose we have an array cities containing the names of several cities, and we want to concatenate them together into a single string separated by commas. We can use the concat() method in a loop like so:

var cities = ["Paris", "Madrid", "Rome"];
var fullList = "";
for (var i = 0; i < cities.length; i++) {
  fullList = fullList.concat(cities[i], ", ");
}
fullList = fullList.slice(0, -2);
Logger.log(fullList);

In this example, we define an array cities containing the names of several cities. We use a for loop to iterate over the array, and use the concat() method to join each city name together with a comma and a space. The resulting string “Paris, Madrid, Rome” is logged to the console.

By using the concat() method in Apps Script, we can easily join two or more strings together into a single string, and use the resulting string in various ways.