toUpperFirst β GTM Variable Template for String
toUpperFirst CORE String
Converts the first character of a string to uppercase and leaves the rest unchanged.
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Examples
Section titled βExamplesβCapitalize first letter
INPUT
String To Convert: hello world
OUTPUT
Hello world
Already capitalized
INPUT
String To Convert: Hello World
OUTPUT
Hello World
Single character
INPUT
String To Convert: a
OUTPUT
A
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.
toUpperFirst
String To Convert
πΎ The string to modify.
Supported formats:
β String
Supported formats:
β String
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.
String To Convert 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.
toUpperFirst()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Converts the first character of a string to uppercase and leaves the rest of the string unchanged. * * @param {string} data.src - The string to modify. * @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 processing. * * @returns {string} The string with the first character converted to uppercase. * * @framework ggLowCodeGTMKit */
const toUpperFirst = function(string) { if (typeof string !== 'string' || string.length === 0) { return ""; } return string.charAt(0).toUpperCase() + string.slice(1);};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// toUpperFirst - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(toUpperFirst(value));// ===============================================================================// toUpperFirst() β Apply Mode// ===============================================================================/*return function(value) { return out(toUpperFirst(value));};*/π§ͺ View Test Scenarios (6 tests)
β
'[example] Capitalize first letter'β
'[example] Already capitalized'β
All uppercase - should return unchangedβ
'[example] Single character'β
Empty string - should return empty stringβ
Object input - should return empty string