transformation
String.prototype.padEnd
Returns a new string padded on the right with padString (default ' ') until it reaches targetLength. Added in ES2017.
str.padEnd(targetLength[, padString]) Parameters
| Parameter | Purpose |
|---|---|
| targetLength | Desired final length in UTF-16 code units |
| padString | String to pad with (default ' ') |
Examples
console.log('5'.padEnd(3, '0')); Prints '500'
console.log('hi'.padEnd(6)); Prints 'hi '
console.log('hello'.padEnd(3, '.')); Prints 'hello' (already long enough)
console.log('a'.padEnd(5, 'xy')); Prints 'axyxy'
Gotcha
Length is measured in UTF-16 code units — a single emoji counts as 2. Padding truncates, not repeats-and-cuts-from-end, to fit.
Related methods
String.prototype.padStart
Returns a new string padded on the left with padString (default ' ') until it reaches targetLength. Added in ES2017; the original is returned unchanged if it is already long enough.
String.prototype.repeat
Returns a new string containing count copies of the original string concatenated together. Added in ES2015; count must be a non-negative finite integer.
String.prototype.trimEnd
Returns a new string with trailing whitespace removed. Added in ES2019 (aliased as trimRight for legacy code).
String.prototype.replace
Returns a new string with the first match of pattern replaced. Pattern can be a string (only the first occurrence) or a RegExp (all occurrences when the g flag is set).
String.prototype.replaceAll
Returns a new string with every occurrence of pattern replaced. Added in ES2021; if pattern is a RegExp it must have the g flag or replaceAll throws TypeError.