absolute β GTM Variable Template for Number
absolute EXTENDED Number
Returns the absolute (non-negative) value of a number.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβPositive stays positive
INPUT
Number: 42
OUTPUT
42
Negative to positive
INPUT
Number: -42
OUTPUT
42
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.
absolute
Number
πΎ The number to calculate the absolute value for.
Supported formats:
β Number
Supported formats:
β Number
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., parse string to number, apply mathematical operations). Internal transformations such as absolute value calculation will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., num => num + ' units', val => val.toFixed(2) for decimal formatting). Useful for chaining transformations on the output.
Number number
π‘ 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.
absolute()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Returns the absolute value of the provided input. * * @param {number} data.num - The number to calculate the absolute value for. * @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 num before calculation. * * @returns {number} The absolute value of the input number. * * @framework ggLowCodeGTMKit */const Math = require('Math');const absolute = function(num) { return Math.abs(num);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// absolute - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const num = applyCast(data.pre, data.num);return out(absolute(num));// ===============================================================================// absolute() β Apply Mode// ===============================================================================/*return function(num) { return out(absolute(num));};*/π§ͺ View Test Scenarios (9 tests)
β
'[example] Positive stays positive'β
'[example] Negative to positive'β
Zero - should return 0β
Negative decimal - should return positive decimalβ
String negative number - should convert and return positive valueβ
Null input - should return 0β
Undefined input - should return NaNβ
Empty string - should return 0β
Array with single string number - should convert and return positive value