JavaScript Reference ยท String
String.prototype.padEnd()
Reference for JavaScript String.prototype.padEnd(): extend a string to a target length with spaces or custom padding, align text columns and understand truncation.
The padEnd() method extends a string to a target length by adding padding at the end. If the string is already that long or longer, it is returned unchanged.
Use it to line up columns of text: names padded to the same width, dotted leaders in a menu, or fixed-width fields in plain-text output. Its counterpart for the start of the string is padStart().
"cat".padEnd(6, "."); // "cat..."
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
string.padEnd(targetLength) string.padEnd(targetLength, padString)
| Name | Description |
|---|---|
targetLength |
The length the result should have. If it is not greater than the string's length, no padding is added. It is converted to an integer. |
padString |
Optional. The text used for padding, repeated and cut as needed. Defaults to a single space. An empty string adds nothing. |
Return value: a new string of at least targetLength code units with the padding added at the end, or the original string if no padding applies.
Exceptions: none for ordinary values. A very large targetLength can throw a RangeError because the string would be too long.
Usage notes
- The result is exactly
targetLengthlong, notstring.length + targetLength. The pad string is repeated and cut at the end, so"abc".padEnd(10, "12345")givesabc1234512. - It never shortens or truncates the original. To force a maximum, combine with
slice():text.slice(0, 10).padEnd(10). - It is a string method. Numbers need converting first with
String(n)or a template literal;(42).padEndisundefined. - Lengths count UTF-16 code units, not what a reader sees. Emoji and combining marks make columns look uneven. Fixed-width alignment is only reliable for plain ASCII text in a monospaced font.
- For HTML, prefer CSS. Table cells, flexbox or grid align columns better than spaces, which the browser collapses.
- Typical uses: text reports in the console, fixed-width file formats, progress bars like
"#".repeat(n).padEnd(10, "-"). - Left or right? Text is usually padded at the end and numbers at the start so the digits line up on the right.
Browser support
padEnd() was added in ES2017 and works in all current browsers.