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 »
Pad a string with "x" until it reaches the length 4:
let text = "5";
let padded = text.padStart(4,"x");
Try it Yourself »
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 »
Syntax
string.padStart(length, string)
Parameters
Parameter | Description |
length | Required. The length of the resulting string. |
string | Optional. 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 an ECMAScript7 (ES7) feature.
ES7 (JavaScript 2016) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
padStart()
is not supported in internet Explorer or Edge 17 (or earlier).