The Array method reverse() in Apps Script
The Array method reverse()
reverses the order of elements in an array.
Prerequisites
This tutorial assumes that you're familiar with:
Syntaxarray.reverse()
Parameters
array.reverse()
The reverse()
method does not accept any parameters.
Return value
The reverse()
method returns a reference to the array that was reversed.
Warning
The reverse()
method reverses an array in place. This means that the array the reverse()
method is called on will itself be reversed and a reference to it will be returned. This behavior will become clear when you read the examples below.
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: The reverse() method reverses an array in place
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.
In the code below, the array colors
is reversed. Please note that the reverse()
method, reverses the array in place - i.e., the colors
array is itself reversed.
var colors = ["red", "blue", "green", "black", "orange", "purple"];
colors.reverse();
Logger.log(colors);
Output
[purple, orange, black, green, blue, red]
Example 2: The reverse() method returns a reference to the array that was reversed
The reverse()
method does not return a new array. It only returns a reference to the array that was reversed. This is an important concept to understand to ensure your code does not result in unintended consequences.
For example, In the code below, the variable reversedColors
contains a reference to the colors
array. It is not a new array. So, by adding a new item to the reversedColors
array, you're actually adding an item to the colors
array. This may not have been your intention and this behavior could lead to unintended consequences or bugs in your program.
var colors = ["red", "blue", "green", "black", "orange", "purple"];
var reversedColors = colors.reverse();
Logger.log(colors);
Logger.log(reversedColors);
Logger.log(colors === reversedColors);
//Add an item to the reversedColors array
reversedColors.push("indigo");
Logger.log(colors);
Logger.log(reversedColors);
Output
[purple, orange, black, green, blue, red]
[purple, orange, black, green, blue, red]
true
[purple, orange, black, green, blue, red, indigo]
[purple, orange, black, green, blue, red, indigo]
Example 3: In the case of two-dimensional arrays, the reverse() method will only apply one level deep. It will not reverse the contents of the inner arrays
The array twoDArray
has two inner arrays: [1,2]
and [3,4]
. The reverse method will not reverse these inner arrays.
var twoDArray = [[1,2],[3,4]];
Logger.log(twoDArray);
Logger.log(twoDArray.reverse());
Output
[[1.0, 2.0], [3.0, 4.0]]
[[3.0, 4.0], [1.0, 2.0]]
Conclusion
In this tutorial, you learned how to use the reverse()
method of an array to reverse its contents. Please remember that the reverse()
method reverses the array in place. Also, it does not return a new array. Instead, it returns a reference to the array that was reversed.
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!