Aller au contenu principal

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'