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'