String.prototype.padEnd
Pads a string on the right to the desired length.
Available since: ES2017
Syntax
str.padEnd(targetLength, padString)Parameters
| Parameter | Type | Description |
|---|---|---|
| targetLength | number | The desired length of the string. |
| padString | string | Optional. What to pad with. A space by default. |
Return value
stringThe string padded on the right to the desired length.
Examples
Padding with dots:
console.log('Tea'.padEnd(8, '.') + '$3');Output:
Tea.....$3Aligning columns in a plain-text table:
const items = [['Tea', 3], ['Coffee', 5]];
for (const [name, price] of items) {
console.log(`|${name.padEnd(8)}|${String(price).padStart(3)}|`);
}Output:
|Tea | 3|
|Coffee | 5|Notes
If the string is already longer than targetLength, it is returned unchanged — the method never cuts anything.
See also
- String.prototype.padStart — Pads a string on the left to a given length.
- String.prototype.repeat — Repeats a string a given number of times.
Related recipes
In the tutorial
- String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.