String.prototype.padStart()
Pads the start of a string with another string until it reaches a target length.
Syntax
str.padStart(targetLength, padString) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
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
| Environment | Since version |
|---|---|
| chrome | 57 |
| firefox | 48 |
| safari | 10 |
| edge | 15 |
| node | 8.0 |