Skip to content

padEnd — GTM Variable Template for String

VARIABLES › STRING
padEnd CORE String

Pad a string or number from the right with a specific character to ensure it reaches a defined total length.


When to Use This

String Manipulation

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


Examples

Pad number with trailing zeros
INPUT
String To Pad: 123
Target Length: 5
Pad Character: 0
OUTPUT
12300
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.

padEnd
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
padEnd()


Under the Hood

📜 View Implementation Code
/**
* Pads the end 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|string} 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 padEnd = 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 inputStr + padding;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// padEnd - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(padEnd(value, data.len, data.chr));
// ===============================================================================
// padEnd(...) – Apply Mode
// ===============================================================================
/*
return function(value, targetLength, padChar) {
   targetLength = data.rp1 ? data.len : targetLength;
   padChar = data.rp2 ? data.chr : padChar;
   return out(padEnd(value, targetLength, padChar));
};
*/
🧪 View Test Scenarios (8 tests)
✅ '[example] Pad number with trailing zeros'
✅ Pad string with spaces to length 10 - should pad with spaces at end
✅ '[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 at end
✅ Undefined input - should return empty string
✅ Null input - should return empty string