String.prototype.padStart()

Pads the start of a string with another string until it reaches a target length.

Since ES2017 Spec ↗

Syntax

str.padStart(targetLength, padString)

Parameters

NameTypeRequiredDescription
targetLength number Yes The desired final length. If less than the current length, the string is returned unchanged.
padString string No The string to pad with, repeated as needed and truncated. Defaults to a space.

Returns

string — A new string padded at the start to the target length.

Examples

console.log('5'.padStart(3, '0'));
Output
005
console.log('abc'.padStart(2, '*'));
Output
abc

Notes

Commonly used for zero-padding numbers or aligning output. The pad string is truncated if it would overflow the target length. Does not mutate the original.

Browser & runtime support

EnvironmentSince version
chrome 57
firefox 48
safari 10
edge 15
node 8.0

See also