JavaScript (ES2026) reference · String Methods

JavaScript String padStart() Method

Pad the start of a string until it reaches a target length, for example zero-padding numbers like 007.

JavaScript (ES2026) Since ES2017

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.padStart(targetLength, padString)

Parameters

ParameterDescription
targetLength The length of the result.
padString optional Text to pad with. Default: " "

Return value

A new string.

Examples

Zero-pad numbersTry it yourself
console.log(String(7).padStart(3, "0"), String(42).padStart(5));

Output

007    42
Mask all but the last fourTry it yourself
const card = "4242424242424242";
console.log(card.slice(-4).padStart(card.length, "*"));

Output

************4242

Version history

VersionChange
JavaScript ES2017Added

In other languages

Was this page helpful?