String.prototype.padEnd()
str.padEnd(targetLength[, padString]) Returns:
string · Added in vES2017 · Updated March 13, 2026 · String Methods string pad alignment formatting es2017
String.prototype.padEnd() pads the current string with a given character (or characters) until it reaches the specified length, appending the padding to the end (right side). This method is essential for creating fixed-width columns in tables, CLI output, and formatted text displays.
Syntax
str.padEnd(targetLength)
str.padEnd(targetLength, padString)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
targetLength | number | — | The length the resulting string should have |
padString | string | " " (space) | The string to pad with |
Return Value
A new string of the specified length, with the padding appended to the end.
Examples
Basic padding
By default, padEnd() uses spaces:
"hello".padEnd(10);
// "hello "
"test".padEnd(6);
// "test "
Custom padding characters
You can specify any character or string to use for padding:
// Pad with zeros
"42".padEnd(5, "0");
// "42000"
// Pad with dots (useful for readability)
"loading".padEnd(10, ".");
// "loading..."
Formatting table data and CLI output
This is where padEnd() shines—creating aligned columns:
const data = [
{ name: "Alice", role: "Admin" },
{ name: "Bob", role: "User" },
{ name: "Charlie", role: "Moderator" }
];
// Print a formatted table
console.log("NAME | ROLE");
console.log("----------|----------");
data.forEach(({ name, role }) => {
console.log(`${name.padEnd(10)}| ${role.padEnd(9)}`);
});
// Output:
// NAME | ROLE
// ----------| ----------
// Alice | Admin
// Bob | User
// Charlie | Moderator
Multi-character padding strings
When the padString is longer than needed, it’s truncated:
// Only uses first character if padString has multiple chars
"ab".padEnd(5, "XYZ");
// "abXYZ"
// padString is truncated to fit
"hi".padEnd(6, "ABC");
// "hiABCA" (only first 4 chars used to reach length 6)
// Useful pattern: cycle through characters
"x".padEnd(8, "ab");
// "xabababa" (repeats and truncates)
Edge Cases
// If targetLength <= string length, returns original
"hello".padEnd(3);
// "hello"
// Empty padString repeats nothing (returns original)
"test".padEnd(8, "");
// "test"
// Unicode characters count as single units
"👍".padEnd(4, "x");
// "👍xxx"
Browser Compatibility
padEnd() was introduced in ES2017 (ECMAScript 8) and is supported in:
- Chrome/Edge 57+
- Firefox 48+
- Safari 11+
- Node.js 8.0+
For older environments, use a polyfill:
if (!StringpadEnd) {
StringpadEnd = function(targetLength, padString) {
targetLength = targetLength >> 0;
padString = String(padString || ' ');
if (this.length > targetLength) return String(this);
return String(this) + padString.repeat(targetLength - this.length);
};
}
See Also
- string::padStart — Pad from the beginning (left side)
- string::repeat — Repeat a string
- string::trimEnd — Remove padding from end