reverse β GTM Variable Template for Array
reverse EXTENDED Array
Returns a new array with the elements in reverse order without modifying the original.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβReverse array
INPUT
Array To Reverse: [1, 2, 3, 4, 5]
OUTPUT
[5, 4, 3, 2, 1]
Non-array returns empty
INPUT
Array To Reverse: not an array
OUTPUT
[]
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
reverse
Array To Reverse
πΎ The array to reverse the order of elements.
Supported formats:
β Array
Supported formats:
β Array
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Array To Reverse array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
reverse()
Related Variables
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Returns a new array with the elements in reverse order.** @param {Array} data.src - The array to reverse.* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before reversing.** @returns {Array} A new array with reversed elements, or an empty array if input is invalid.** @framework ggLowCodeGTMKit*/const getType = require('getType');
const reverse = function(arr) { if (getType(arr) !== 'array') { return []; } return arr.slice().reverse();};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// reverse - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(reverse(value));
// ===============================================================================// reverse() β Apply Mode// ===============================================================================/*return function(value) { return out(reverse(value));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Reverse array'β
Array with strings - should reverse orderβ
Array with single element - should return same elementβ
Empty array - should return empty arrayβ
'[example] Non-array returns empty'