Skip to content

toStringRepresentation — GTM Variable Template for String

VARIABLES › STRING
toStringRepresentation EXTENDED String

Converts the provided value to its string representation. Returns the string "null" for null and "undefined" for undefined.



Examples

Number to string
INPUT
Value To Convert: 42
OUTPUT
42
Null returns \"null\"
INPUT
Value To Convert: null
OUTPUT
null
Undefined returns \"undefined\"
INPUT
Value To Convert: undefined
OUTPUT
undefined

GTM Configuration

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

toStringRepresentation
Value To Convert
💾 The value to convert to a string.

Supported formats:
  ✓ Any
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the value before internal logic (e.g., normalize data, apply calculations). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), str => str.trim() for whitespace removal). Useful for chaining transformations on the output.
Value To Convert any
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
toStringRepresentation()


Under the Hood

📜 View Implementation Code
/**
 * Converts a value to a string.
 *
 * @param {any} data.val - The value to convert to a string.
 * @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 val before conversion.
 * 
 * @returns {string} The string representation of the value, or "null" or "undefined" if the value is null or undefined.
 *
 * @framework ggLowCodeGTMKit
 */

const toStringRepresentation = function(value) {
    if (value === null) { return 'null'; }
    if (value === undefined) { return 'undefined'; }
    return value.toString();
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// toStringRepresentation - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedValue = applyCast(data.pre, data.val);
return out(toStringRepresentation(processedValue));
// ===============================================================================
// toStringRepresentation() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(toStringRepresentation(value));
};
*/
🧪 View Test Scenarios (10 tests)
✅ '[example] Number to string'
✅ Boolean true to string - returns true string
✅ Boolean false to string - returns false string
✅ Array to string - converts array to comma-separated string
✅ Object to string - converts object to string representation
✅ '[example] Null returns \"null\"'
✅ '[example] Undefined returns \"undefined\"'
✅ Empty string - returns empty string
✅ Zero number - converts to 0 string
✅ Negative number - converts to string with minus sign