The Array method join() in Apps Script
The Array method join()
concatenates the values in an array into a single string.
Prerequisites
This tutorial assumes that you're familiar with:
Syntaxarray.join(separator)
Parameters
array.join(separator)
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.
ExamplesIn Apps Script, you can only run functions. So you have to copy and paste each of the code snippets below into a function before you run them.
I recommend creating separate functions to try out each of the examples below. Also, don't forget to give each of these functions a unique name.
Example 1: If the separator is not specified or if it is undefined, a comma will be used as the separator by default
In Apps Script, you can only run functions. So you have to copy and paste each of the code snippets below into a function before you run them.
I recommend creating separate functions to try out each of the examples below. Also, don't forget to give each of these functions a unique name.
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 usedExample 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 linevar colors = ["red", "blue", "green", "black", "orange", "purple"];
Logger.log(colors.join("\n"));
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.
A range of cells spanning multiple rows in Google Sheets is represented as a 2D array in Apps Script. Therefore, understanding what output will be produced when you apply an array method to a 2D array can be very useful when you interact with the data in your spreadsheet scripts.
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
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!