trim — GTM Variable Template for String
trim CORE String
Trims any leading or trailing whitespace from a valid string. Returns an empty string when the input is not a valid string.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Examples
Trim leading and trailing spaces
INPUT
String To Trim: hello world
OUTPUT
hello world
Only spaces returns empty string
INPUT
String To Trim:
OUTPUT
""
Non-string returns empty string
INPUT
String To Trim: 123
OUTPUT
""
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
trim
String To Trim
💾 The value to be converted to a string and trimmed.
Supported formats:
✓ String
Supported formats:
✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., normalize case, convert to string). Internal transformations such as string validation will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), val => val + ' trimmed' for string modifications). Useful for chaining transformations on the output.
String To Trim string
💡 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.
trim()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Trims whitespace from both ends of a string.
*
* @param {any} data.src - The value to be converted 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 src before trimming.
*
* @returns {string} The string representation of the input with whitespace trimmed, or an empty string.
*
* @framework ggLowCodeGTMKit
*/
const trim = function(string) {
return typeof string === 'string' ? string.trim() : "";
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// trim - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(trim(value));
// ===============================================================================
// trim() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(trim(value));
};
*/🧪 View Test Scenarios (9 tests)
✅ Trim both ends
✅ String with only leading spaces - should trim start
✅ String with only trailing spaces - should trim end
✅ String with no whitespace - should return same string
✅ '[example] Trim leading and trailing spaces'
✅ '[example] Only spaces returns empty string'
✅ Trim tabs and newlines
✅ '[example] Non-string returns empty string'
✅ Non-string input (null) - should return empty string