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
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Examples
Section titled β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
""
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.
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
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled β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