Skip to content

undefinedTo — GTM Variable Template for Value

VARIABLES › VALUE
undefinedTo CORE Value

Replaces undefined values with a default. Non-undefined values pass through unchanged.


When to Use This

Value Utilities

Handle edge cases — defaults for undefined/null, type checks, coercion.


Examples

Undefined returns default
INPUT
Value To Check: undefined
Default Value: default
OUTPUT
default
String passes through
INPUT
Value To Check: hello
Default Value: default
OUTPUT
hello

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

undefinedTo
Value To Check
💾 The value to check for undefined.

Supported formats:
  ✓ Any
Default Value
💾 The default value to return if the source value is undefined.

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


Under the Hood

📜 View Implementation Code
/**
 * Replaces undefined 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 undefined.
 * @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 not undefined, otherwise returns the default value.
 *
 * @framework ggLowCodeGTMKit
 */
const undefinedTo = function(value, defaultValue) {
    return value === undefined ? defaultValue : value;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// undefinedTo - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(undefinedTo(value, data.def));
// ===============================================================================
// undefinedTo(...) – Apply Mode
// ===============================================================================
/*
return function(value, defaultValue) {
   defaultValue = data.rp1 ? data.def : defaultValue;
   return out(undefinedTo(value, defaultValue));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Undefined returns default'
✅ '[example] String passes through'
✅ Zero value - returns original zero value
✅ Null value - returns original null value
✅ False value - returns original false value