Google Apps Script:
Concatenating strings

How to:

In Google Apps Script, which is based on JavaScript, there are several ways to concatenate strings. Here are some common methods:

Using the plus operator (+):

var firstName = "John";
var lastName = "Doe";
var fullName = firstName + " " + lastName;
Logger.log(fullName); // Output: John Doe

Using the concat() method:

var string1 = "Hello";
var string2 = "World";
var combinedString = string1.concat(" ", string2);
Logger.log(combinedString); // Output: Hello World

Using template literals (backticks):

This is a modern and flexible way to concatenate strings, allowing you to embed expressions within strings easily.

var language = "Google Apps Script";
var message = `Learning ${language} is fun!`;
Logger.log(message); // Output: Learning Google Apps Script is fun!

Each of these methods has its use cases, and the choice between them typically depends on readability requirements and the complexity of the strings being concatenated.

Deep Dive

String concatenation is a fundamental aspect of not just Google Apps Script but many programming languages. Historically, concatenating strings was often performed using the plus operator or specialized functions/methods like concat(). However, with the introduction of template literals in ECMAScript 2015 (ES6), which Google Apps Script supports, developers have gained a more powerful and intuitive way to deal with strings.

Template literals not only simplify the syntax for embedding expressions within strings but also support multilined strings without the need for explicit newline characters. This reduces the potential for errors and improves code readability, especially when dealing with complex strings or when substituting multiple variables into a text template.

While the + operator and concat() method are still widely used and supported for backward compatibility and simplicity in simpler scenarios, template literals offer a modern, expressive alternative that is often considered superior for string concatenation, particularly when readability and maintainability are of concern.

Nevertheless, it’s important to choose the method that best fits the specific context and requirements of your project, considering factors like the target environment’s compatibility (though this is rarely an issue with Google Apps Script), performance implications (minimal for most applications), and the development team’s familiarity with modern JavaScript features.