Skip to content

charAt — GTM Variable Template for String

VARIABLES › STRING
charAt EXTENDED String

Returns the character at a specified index in a valid string.



Examples

Character at index 1
INPUT
String: Hello
Index: 1
OUTPUT
e
First character
INPUT
String: World
Index: 0
OUTPUT
W
Index out of bounds
INPUT
String: Hello
Index: 10
OUTPUT
""

GTM Configuration

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

charAt
String
Returns the character at a specified index in a valid string.

*** Character at index 1***
Input: String: Hello
Index: 1

↪️ Output: e

*** First character***
Input: String: World
Index: 0

↪️ Output: W

*** Index out of bounds***
Input: String: Hello
Index: 10
Index
🔢 The index position of the character to retrieve.

Supported formats:
  ✓ Number
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 string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Index number
charAt()


Under the Hood

📜 View Implementation Code
/**
 * Returns the character at a specified index in a valid string.
 * 
 * @param {string} data.str - The string to retrieve the character from.
 * @param {number} data.idx - The index of the character to retrieve.
 * @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 input before processing.
 * 
 * @returns {string} The character at the specified index.
 *
 * @framework ggLowCodeGTMKit
 */
const charAt = function(input, index) {
    if (typeof input !== 'string' || index < 0 || index >= input.length) {
        return '';
    }
    return input.charAt(index);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// charAt - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedStr = applyCast(data.pre, data.str);
return out(charAt(processedStr, data.idx));
// ===============================================================================
// charAt(...) – Apply Mode
// ===============================================================================
/*
return function(value, index) {
    index = data.rp1 ? data.idx : index;
    return out(charAt(value, index));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Character at index 1'
✅ '[example] First character'
✅ Last character - should return last char
✅ '[example] Index out of bounds'
✅ Negative index - should return empty string