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

JavaScript · Function

padStart

String.prototype.padStart() adds characters to the beginning of a string until it reaches a requested length. The optional padding text is repeated or truncated as needed.

Since
ECMAScript 2017

How it works

Call padStart() on a string with the desired final length and, optionally, the text used for padding. If padding is needed, the padding text is repeated and shortened as necessary, then placed before the original string; otherwise the original string is returned unchanged.

Syntax

padStart(targetLength)
padStart(targetLength, padString)

Parameters

NameTypeDescription
targetLengthnumberDesired length of the resulting string after padding. Values no greater than the current string length leave it unchanged.
padStringstringOptional text used for padding; it is repeated or shortened as needed. The default is a space.

Returns

A String of the specified targetLength with padString applied at the start.

Key points

  • Added characters appear before the existing content, making the method useful for left-aligned padding.
  • targetLength describes the final string length rather than the number of padding characters to add.
  • No padding occurs when the requested length is already satisfied by the original string.
  • padString supplies the characters used to fill the missing length.
  • The return value is the completed string with leading padding.

Common mistakes

  • A padding string that would exceed the requested final length is cut off from its end.
  • Omitting the second argument inserts spaces, not zeroes or another numeric-style fill character.

Example: Pad a numeric identifier

const invoiceId = String(42).padStart(6, "0");
console.log(invoiceId); // "000042"

Converts a number to text and adds leading zeroes until the text reaches six characters.

When to use it

Zero-padded identifiers

Formatting identifiers such as invoice numbers or database keys with a consistent minimum width.

Text alignment

Creating fixed-width labels or columns in logs and terminal output.

Clock and counter formatting

Ensuring displayed counters have a consistent number of visible positions.

Interview questions

QWhat does String.prototype.padStart() do?

padStart() returns a string whose length is at least the requested targetLength, adding the chosen padding at the beginning when necessary. If the original string already meets or exceeds that length, it is returned unchanged.

Follow-up: What happens when the padding text cannot fit completely? It is repeated as needed and truncated from the end so the result reaches the requested length.

QWhat are padStart()'s parameters?

The first argument is targetLength, the desired length after padding. The optional second argument is padString; if omitted, a space is used.

Follow-up: Why should a numeric value be converted first? padStart() is a String method, so String(value) or value.toString() is needed before calling it.

QDoes padStart() modify the string it is called on?

The method does not mutate the original string because strings are immutable. It produces and returns a new padded string when padding is required, or the original string value when no padding is needed.

Follow-up: How is an oversized padString handled? Only the portion needed to reach targetLength is retained, with truncation from the pad string's end.

  • String.prototype.padEnd()
  • String.prototype.trimStart()
  • String.prototype.repeat()
  • String

Source: String.prototype.padStart () - JavaScript | MDN. Checked 2026-10-15.