Skip to content

falsyTo — GTM Variable Template for Value

VARIABLES › 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

Conditional Logic

Branch logic based on conditions — if/else, ternary, boolean gates.


Examples

Truthy value returns original
INPUT
Value To Check: hello
Default Value: default
OUTPUT
hello
Empty string returns default
INPUT
Value To Check:
Default Value: default
OUTPUT
default
Null returns default
INPUT
Value To Check: null
Default Value: fallback
OUTPUT
fallback

GTM Configuration

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
Default Value
💾 The default value to return if the source value is falsy.

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
falsyTo()


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 ? data.def : defaultValue;
  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