Skip to content

padStart — GTM Variable Template for String

VARIABLES › STRING
padStart CORE String

Pad a string or number from the left with a specific character to ensure it reaches a defined total length, useful for formatting values.


When to Use This

String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Formatting

Normalize casing, spacing, encoding, and presentation of data values.


Examples

Pad number with leading zeros
INPUT
String To Pad: 123
Target Length: 5
Pad Character: 0
OUTPUT
00123
String already exceeds target length
INPUT
String To Pad: hello world
Target Length: 5
Pad Character: 0
OUTPUT
hello world
Pad with custom character
INPUT
String To Pad: 42
Target Length: 6
Pad Character: *
OUTPUT
****42

GTM Configuration

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

padStart
String To Pad
💾 The input string or number to pad.

Supported formats:
  ✓ String
  ✓ Number
Target Length
💾 The final desired length of the output string.

Supported formats:
  ✓ String
  ✓ Number
Pad Character
💾 The character to use for padding (default is "0").

Supported formats:
  ✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert number to string, normalize format). Internal transformations such as string conversion 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 + ' formatted' for string modifications). Useful for chaining transformations on the output.
String To Pad number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Target Length number
Pad Character number
padStart()


Under the Hood

📜 View Implementation Code
/**
 * Pads the start of a string with a given character until it reaches the desired length.
 * 
 * @param {string|number} data.src - The input string or number to pad.
 * @param {number} data.len - The final desired length of the output string.
 * @param {string} data.chr - The character to use for padding (default is "0").
 * @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 padding.
 * 
 * @returns {string} The padded string.
 *
 * @framework ggLowCodeGTMKit
 */
const makeNumber = require('makeNumber');
const padStart = function(input, targetLength, padChar) {
    if (input === null || input === undefined) return '';
    
    const pad = (padChar !== undefined && padChar !== null && padChar !== '') ? padChar : '0';
    const inputStr = input.toString();
    const len = makeNumber(targetLength);
    
    if (typeof len !== 'number' || len < 0) return inputStr;
    
    const padNeeded = len - inputStr.length;
    if (padNeeded <= 0) return inputStr;
    
    let padding = '';
    for (let i = 0; i < padNeeded; i++) {
        padding += pad;
    }
    return padding + inputStr;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// padStart - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(padStart(value, data.len, data.chr));
// ===============================================================================
// padStart(...) – Apply Mode
// ===============================================================================
/*
return function(value, targetLength, padChar) {
   targetLength = data.rp1 ? data.len : targetLength;
   padChar = data.rp2 ? data.chr : padChar;
   return out(padStart(value, targetLength, padChar));
};
*/
🧪 View Test Scenarios (8 tests)
✅ '[example] Pad number with leading zeros'
✅ Pad string with spaces to length 10 - should pad with spaces
✅ '[example] String already exceeds target length'
✅ '[example] Pad with custom character'
✅ Empty string with padding - should create padding only
✅ Object input - should convert to string then pad
✅ Undefined input - should return empty stringUntitled test 8
✅ Null input - should return empty string