The Array method join() in Apps Script

The Array method join() concatenates the values in an array into a single string.

Syntax

array.join(separator)

Parameters

separator (optional):

  • If specified, the array's elements are separated using this value in the concatenated string.

  • If it is not specified explicitly, a comma (,) will be used as the separator by default.

  • If the separator is not a string value, it will be converted to a string before being used.

Return value

A string value formed by concatenating all the elements in the array separated by the separator.

Examples

Example 1: If the separator is not specified or if it is undefined, a comma will be used as the separator by default

Example 1.1

In the code below, the separator has not been specified so a comma is used as the separator by default.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join());

Output

red,blue,green,black,orange,purple

Example 1.2

In the code below, the separator is undefined so a comma is used as the separator by default.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join(undefined));

Output

red,blue,green,black,orange,purple

Example 2: You can explicitly specify the separator

In the code below, the string "/" is specified as the separator.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join("/"));

Output

red/blue/green/black/orange/purple

Example 3: If the separator you specify is not a string, it will be converted to a string before being used

Example 3.1

In the code below, the number 0 is converted to the string "0" before being used.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join(0));

Output

red0blue0green0black0orange0purple

Example 3.2

In the code below, the array ["a"] is converted to the string "a" before being used.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join(["a"]));

Output

redablueagreenablackaorangeapurple

Example 3.3

In the code below, the array ["a","b"] is converted to the string "a,b" before being used.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join(["a","b"]));

Output

reda,bbluea,bgreena,bblacka,borangea,bpurple

Example 4: If you specify an empty string ("") as the separator, the array elements will be concatenated together without any separation between them

In the code below, an empty string ("") is used as the separator. Notice that the output is a string where the array elements have been concatenated together without any separation between them.

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join(""));

Output

redbluegreenblackorangepurple

Example 5: When the separator is a newline ("\n"), each array element in the concatenated string will be displayed on a new line

var colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join("\n"));

Output

red

blue

green

black

orange

purple

Example 6: When the array is a 2D array, all of its elements will be concatenated together into a single string.

In the code below, the array twoDArr is a two-dimensional array. The join() method concatenates all of its elements into a single string. The elements in the first row will be concatenated first, followed by the elements in the second row, and so on.

var twoDArr = [[1,2],[3,4]];
Logger.log(twoDArr.join());

Output

1,2,3,4

Example 7: If the values in the array are not strings, they will be converted to a string before being concatenated

In the code below, the array objArr contains objects. These objects are converted into the string "[object Object]" before being concatenated.

var objArr = [{firstName: "Jack", lastName: "Ryan"},{firstName: "Paige", lastName: "Potter"}];
Logger.log(objArr.join());

Output

[object Object],[object Object]

Conclusion

In this tutorial, you learned how to use the join() method of an array to concatenate its values into a single string.

Master Google Sheets Automation

Sign up to receive exclusive Automation tips and tutorials!
I'll send you actionable tips on automating tasks with Google Sheets and coding with Apps Script. You'll also be notified whenever new content is published.
PLUS: Get instant access to my Birthday Reminder Template! 🎂
By signing up you agree to the Privacy Policy & Terms.

Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!