falsyTo β GTM Variable Template for Value
falsyTo CORE Value
Replaces falsy values (false, 0, "", null, undefined, NaN) with a default value. Returns the original if truthy, otherwise returns the default.
When to Use This
Section titled βWhen to Use ThisβConditional Logic
Branch logic based on conditions β if/else, ternary, boolean gates.
Examples
Section titled βExamplesβTruthy value returns original
INPUT
Value To Check: hello
Default Value: default
Default Value: default
OUTPUT
hello
Empty string returns default
INPUT
Value To Check:
Default Value: default
Default Value: default
OUTPUT
default
Null returns default
INPUT
Value To Check: null
Default Value: fallback
Default Value: fallback
OUTPUT
fallback
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.
falsyTo
Value To Check
πΎ The value to check for falsy values.
Supported formats:
β Any
Supported formats:
β Any
Default Value
πΎ The default value to return if the source value is falsy.
Supported formats:
β Any
Supported formats:
β Any
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.
Value To Check any
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Default Value any
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
falsyTo()
Related Variables
Section titled βRelated VariablesβSame category: Value
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Replaces falsy values with a specific default value. * * @param {any} data.src - The value to check. * @param {any} data.def - The default value to return if the value is falsy. * @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 checking. * * @returns {any} Returns the original value if it's truthy, otherwise returns the default value. * * @framework ggLowCodeGTMKit */const falsyTo = function(value, defaultValue) { return value || defaultValue;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// falsyTo - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(falsyTo(value, data.def));// ===============================================================================// falsyTo(...) β Apply Mode// ===============================================================================/*return function(value, defaultValue) { defaultValue = data.rp1 ? defaultValue : data.def; return out(falsyTo(value, defaultValue));};*/π§ͺ View Test Scenarios (10 tests)
β
'[example] Truthy value returns original'β
'[example] Empty string returns default'β
'[example] Null returns default'β
Falsy value (undefined) - should return defaultβ
Falsy value (0) - should return defaultβ
Falsy value (false) - should return defaultβ
Truthy number - should return originalβ
Truthy object - should return originalβ
Empty array (truthy) - should return originalβ
String with spaces (truthy) - should return original