Align text or numeric values in console and report output.
Formatting columns
JavaScript · Function
String.prototype.padEnd() appends repeated and, if necessary, truncated filler so a string reaches a requested length. It leaves strings already at least that long unchanged.
Call padEnd on a string with the desired final length and, optionally, a filler string. If the original string is already at least that long, it is returned unchanged; otherwise the filler is repeated as needed and cut off to fit exactly at the end.
padEnd(targetLength)
padEnd(targetLength, padString)| Name | Type | Description |
|---|---|---|
targetLength | number | The desired length of the resulting string. If it is no greater than the current string length, the string is unchanged. |
padString | string | The string used for padding. It is optional, defaults to a space, and is truncated if necessary. |
A new string padded at the end to the specified target length, or the original string when no padding is needed.
const label = "ID";
const value = "42";
console.log(`${label}:`.padEnd(8, "-") + value);
// "ID:-----42"The label is extended on its right with hyphens until it reaches eight characters, then the value is appended.
Formatting columns
Fixed-width records
Readable identifiers
padEnd(targetLength, padString) returns a new string whose length is targetLength when padding is needed. Padding is appended to the original string and may be repeated or truncated to fit.
Follow-up: What happens when targetLength is no greater than the original length? The original string is returned unchanged.
targetLength specifies the desired resulting string length. padString is optional; when omitted, a space is used.
Follow-up: What if padString cannot fit completely? It is truncated from the end so the result does not exceed targetLength.
The method returns a String with padding applied at the end. For example, "abc".padEnd(6, "123456") produces "abc123".
Follow-up: What does "abc".padEnd(1) produce? It produces "abc", because a smaller target does not shorten the source.
String.prototype.padStartString.prototype.repeatString.prototype.trimEnd