JavaScript String padStart()
Examples
Pad a string with "0" until it reaches the length 4:
let text = "5";
let padded = text.padStart(4,"0");
Try it Yourself »
let padded = text.padStart(4,"0");
Pad a string with "x" until it reaches the length 4:
let text = "5";
let padded = text.padStart(4,"x");
Try it Yourself »
let padded = text.padStart(4,"x");
Description
The padStart()
method pads a string from the start.
The padStart()
method pads a string with another string (multiple times) until it reaches a given length.
See Also:
Note
The padStart()
method is a string method.
To pad a number, convert the number to a string first.
See the example below.
Example
let numb = 5;
let text = numb.toString();
let padded = text.padStart(4,"0");
Try it Yourself »
let text = numb.toString();
let padded = text.padStart(4,"0");
Syntax
string.padStart(length, string)
Parameters
Parameter
Description
length Required.
The length of the resulting string.
The length of the resulting string.
string Optional.
The string to pad with.
Default is space.
The string to pad with.
Default is space.
Return Value
Type
Description
A string A String of the specified length, with the padding applied from the start.
Browser Support
padStart()
is a JaveScript 2017 feature.
ES 2017 is supported in all modern browsers since September 2017:
Chrome 58 | Edge 15 | Firefox 52 | Safari 11 | Opera 45 |
Apr 2017 | Apr 2017 | Mar 2017 | Sep 2017 | May 2017 |