Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.padEnd — padding the end of a string

String.prototype.padEnd pads the end of a string to a target length by repeating a supplied string or default spaces. It leaves the original unchanged and never shortens a string that is already long enough.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

str.padEnd(maxLength[, fillString])

String.prototype.padEnd returns a string padded on the right to maxLength. This argument specifies the total target length, not the number of characters to add. The method leaves the original unchanged. It has been part of ECMAScript since the 2017 edition; it is not a Web API. The specification alone does not establish support in a particular browser.

When fillString is omitted or is undefined, the padding is a U+0020 space. Otherwise, the padding value is converted to a string, repeated, and truncated at its end if necessary. An empty fillString adds nothing. If maxLength does not exceed the input length, the method returns the string unchanged without converting fillString.

maxLength undergoes ToLength: a positive fractional part is discarded, while negative values and NaN become zero. Length is measured in UTF-16 code units, not visible characters or screen columns. Truncating padding can split a surrogate pair, so this method does not guarantee correct alignment of arbitrary Unicode text.

The example demonstrates repeating and truncating padding, default spaces, and two cases without padding. JSON.stringify makes the string boundaries visible so that trailing spaces can be counted.

Examples

const label = "ID";
console.log(JSON.stringify(label.padEnd(7, ".-")));
console.log(JSON.stringify(label.padEnd(4)));
console.log(JSON.stringify(label.padEnd(1, "0")));
console.log(JSON.stringify(label.padEnd(7, "")));
console.log(label);

Result

"ID.-.-."
"ID  "
"ID"
"ID"
ID

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources