padEnd()
padEnd(
str,length,padString):string
Pads the end of a string with a given character until it reaches the specified length.
DEPRECATED
Use string.padEnd() directly instead.
Parametersโ
str: string | null | undefinedโ
The string to pad.
length: number | null | undefinedโ
The target length of the padded string.
padString: string = " "โ
The string to use for padding.
Returns: stringโ
The padded string.
See Alsoโ
Sinceโ
2.0.0
Also known asโ
padEnd (Lodash, es-toolkit) ยท โ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleโ
// โ Deprecated approach
padEnd('hello', 10); // => 'hello '
padEnd('hello', 10, '0'); // => 'hello00000'
// โ
Recommended approach
'hello'.padEnd(10); // => 'hello '
'hello'.padEnd(10, '0'); // => 'hello00000'
How it works?โ
Pads the end of a string to a target length.
Deprecated: Use string.padEnd() directly (ES2017).
Native Equivalentโ
// โ padEnd('hello', 10, '.')
// โ
'hello'.padEnd(10, '.')
Use Casesโ
Pad end ๐โ
Pad string at end to target length.
'5'.padEnd(3, '0'); // '500'
Align columnsโ
Align text in columns.
const label = name.padEnd(20, ' ');
console.log(`${label}${value}`);
Format currency amounts with trailing zerosโ
Ensure decimal amounts always display the expected number of digits.
const formatPrice = (amount: string) => {
const [integer, decimal = ''] = amount.split('.');
return `${integer}.${decimal.padEnd(2, '0')}`;
};
formatPrice('9.5'); // '9.50'
formatPrice('12'); // '12.00'