String.prototype.padEnd()
The mirror of padStart, and the one you want for left-aligned columns. Same two rules: it never shortens, and a multi-character pad is cut to fit.
Demo
The quotes around the output matter here — trailing spaces are otherwise invisible, and they are exactly what this method adds. A dot leader shows that the pad need not be whitespace at all. The third case is the shared rule with padStart: a string longer than the target comes back unchanged, so this cannot enforce a column width on its own. The multi-character pad repeats then gets cut, so 'ab' to length 5 gives 'xabab'.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| targetLength | number | yes | Desired final length. At or below the current length, nothing happens. |
| padString | string | no (' ') | Text to pad with, repeated and truncated to fit exactly. An empty pad string does nothing. |
Return value
string — A new string of at least targetLength, padded on the RIGHT. Like padStart it never truncates.
Common patterns
rows.forEach(r => console.log(r.name.padEnd(16) + r.value));
title.padEnd(40, '.') + page;
const cell = s.slice(0, 16).padEnd(16);
Examples
Pitfalls
'a'.padEnd(4) === 'a'
'a'.padEnd(4).trim() === 'a'
'a very long value'.padEnd(8)
'a very long value'.slice(0, 8).padEnd(8)
div.textContent = 'a'.padEnd(10) + 'b'
display: grid
(5).padEnd(3)
String(5).padEnd(3)
When to use
- Left-aligned columns in logs, terminals or pre blocks
- Dot leaders and other plain-text decoration
- Fixed-width record formats
- Padding numbers on the left → padStart
- Aligning in HTML → CSS grid, flex or a table
- You need a maximum width → slice first
- The value will be stored or compared → pad only at output
Notes
FAQ
padEnd for text you want left-aligned — labels, names, anything read left to right. padStart for numbers, which align on their right-hand digit, and for zero-padded identifiers.
name.padEnd(12); // 'Alice ' String(n).padStart(4, '0'); // '0042'