JavaScript String padStart()

Definition and usage

padStart() The method is used to pad characters at the beginning of the string.

padStart() The method uses another string (repeated multiple times) to pad the current string until it reaches the specified length.

Also see:

padEnd() method

trim() method

trimEnd() method

trimStart() method

Note

padStart() The method is a string method.

If you need to pad a number, you need to convert the number to a string first.

See the following examples.

Instance

Example 1

Pad the string with "0" until its length reaches 4:

let text = "5";
let padded = text.padStart(4,"0");

Try it yourself

Example 2

Pad the string with "x" until its length reaches 4:

let text = "5";
let padded = text.padStart(4,"x");

Try it yourself

Example 3

let numb = 5;
let text = numb.toString();
let padded = text.padStart(4,"0");

Try it yourself

Syntax

string.padStart(length, string)

Parameter

Parameter Description
length Required. The target length of the padded string.
string Optional. The string to be used for padding. The default is a space.

Return value

Type Description
String Returns a string of specified length, padded with specified characters at the beginning.

Browser support

padStart() It is a feature of ECMAScript 2017.

Since September 2017, all modern browsers support ES2017:

Chrome Edge Firefox Safari Opera
Chrome 58 Edge 15 Firefox 52 Safari 11 Opera 45
April 2017 April 2017 March 2017 September 2017 May 2017

Internet Explorer does not support padStart().