Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

padEnd

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.

How it works

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.

Syntax

padEnd(targetLength)
padEnd(targetLength, padString)

Parameters

NameTypeDescription
targetLengthnumberThe desired length of the resulting string. If it is no greater than the current string length, the string is unchanged.
padStringstringThe string used for padding. It is optional, defaults to a space, and is truncated if necessary.

Returns

A new string padded at the end to the specified target length, or the original string when no padding is needed.

Key points

  • It extends the receiver by adding a chosen filler string on its right.
  • Unlike padStart, the added characters follow the original content.
  • The target length controls the final number of UTF-16 code units.
  • targetLength describes the length after padding, not the number of characters to add.
  • Its return value is the padded string rather than a separate padding object.
  • A filler can repeat and then be partially used to reach the exact target.

Common mistakes

  • Omitting padString adds spaces, which can be visually subtle and easy to overlook in output.
  • The filler may be cut in the middle of a repetition, so the requested final length takes priority over preserving complete filler copies.
  • A target shorter than the original does not truncate the original string.

Example: Align a label before a value

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.

When to use it

Align text or numeric values in console and report output.

Formatting columns

Create fields with a consistent width before serialization.

Fixed-width records

Add visible trailing markers or separators to short labels.

Readable identifiers

Interview questions

QWhat does String.prototype.padEnd do?

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.

QWhat are the method's parameters?

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.

QHow does padEnd handle oversized filler or a smaller target?

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.padStart
  • String.prototype.repeat
  • String.prototype.trimEnd

Source: String.prototype.padEnd(). Checked 2026-10-15.