The Array method concat() in Apps Script

The Array method concat() merges (i.e., concatenates) two or more arrays into a single array.

Syntax

var mergedArray = array1.concat(array2, array3, … arrayN)

Parameters

One or more arrays or values to be merged into a single array. These values are merged in the same order as they appear in the statement. The original arrays and values will not be modified by the concat() method. Instead, a new array will be created.

Return value

The merged array.

Examples

Example 1: Merging two arrays

In the code below, two arrays colors1 and colors2 are merged into a single array called colors.

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

Output

[red, blue, green, black, orange, purple]

Example 2: Merging multiple arrays

In the code below, three arrays are merged into a single array called colors.

var colors1 = ["red", "blue", "green"];
var colors2 = ["black", "orange", "purple"];
var colors3 = ["indigo"]
var colors = colors1.concat(colors2, colors3);
Logger.log(colors);

Output

[red, blue, green, black, orange, purple, indigo]

Example 3: Merging an array and a value

In the code below, the array colors1 and the value "black" are merged into a single array called colors.

var colors1 = ["red", "blue", "green"];
var colors = colors1.concat("black");
Logger.log(colors);

Output

[red, blue, green, black]

Example 4: When you merge an array with other arrays and values at the same time, the resulting array will contain all of the values in the same order that you specified

In the code below, the array colors1, the value "black" and another array ["orange", "purple"] are merged into a single array called colors.

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

Output

[red, blue, green, black, orange, purple]

Example 5: When you concat an array with an empty array, the resulting array will have the same elements as the original array

In the code below, the array colors1 is merged with an empty array. The resulting array colors will have the same elements as the original array colors1.

var colors1 = ["red", "blue", "green"];
var colors = colors1.concat([]);
Logger.log(colors);

Output

[red, blue, green]

Conclusion

In this tutorial, you learned how to use the concat() method to merge two or more arrays into a single array.

Sign up to be notified when I publish new content

By signing up you agree to the Privacy Policy & Terms.