Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.padEnd

Pads a string on the right to the desired length.

Available since: ES2017

Syntax

str.padEnd(targetLength, padString)

Parameters

ParameterTypeDescription
targetLengthnumberThe desired length of the string.
padStringstringOptional. 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.....$3

Aligning 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

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.