The JavaScript string.padEnd() method
The padEnd() method adds characters to the end of a string until it reaches a target length, then returns it. "5".padEnd(3, "0") gives "500". It's used to left-align text in fixed-width columns. Its mirror, padStart(), pads the front.
Overview
padEnd() makes a string a minimum length by adding padding to the end. You give it the target length and, optionally, what to pad with (a space by default). If the string is already that long, it comes back unchanged.
Its main use is left-aligning text in monospaced output — building a simple table where labels line up. "Name".padEnd(10) gives "Name ", so the next column starts at a fixed position. It's the layout counterpart to its sibling padStart(), which pads the front to right-align (great for numbers).
Both return a new string and leave the original alone, and the pad string repeats as needed (truncated if it would overshoot). Since numbers don't have these methods, convert with String(n) first when padding numeric values.
Syntax
str.padEnd(targetLength)
str.padEnd(targetLength, padString)
"Name".padEnd(10) // "Name "
"5".padEnd(3, "0") // "500"
Parameters
The string.padEnd() method accepts the following parameters.
| Parameter | Description |
|---|---|
targetLength |
The length the result should reach. If already this long, the string is returned unchanged. |
padString |
Optional. The text to pad with, repeated as needed. Defaults to a space. |
Example
<pre id="out" style="font:14px ui-monospace,monospace"></pre>
<script>
const rows = [['Apple', 3], ['Banana', 12], ['Fig', 7]];
const text = rows
.map(r => r[0].padEnd(10) + r[1])
.join('\n');
document.getElementById('out').textContent = text;
</script>
Best practices
- Use
padEnd()to left-align text in fixed-width, monospaced output. - Use padStart() to right-align (e.g. numbers).
- Convert numbers with
String(n)before padding. - A string already at or beyond the target length is returned unchanged.
Frequently asked questions
What is the difference between padEnd() and padStart()?
padEnd() pads the end of a string (left-aligns the text); padStart() pads the start (right-aligns it).How do I align text into columns?
padEnd(), so the next column always starts at the same position in monospaced text.What does padEnd() do if the string is already long enough?
Can I pad with a custom character?
"5".padEnd(3, "*") gives "5**".