JavaScript String.prototype.padStart() Method
The padStart() method pads the start of a string with another string until it reaches a given length, such as adding leading zeros.
Description
The padStart() method adds characters to the start of a string until it reaches the target length. The classic use is adding leading zeros, as in times and dates, or masking all but the last digits of a card number.
If the string is already as long as the target length, or longer, it's returned unchanged; it's never truncated. A pad string of several characters is repeated and cut to fit, so '7'.padStart(5, 'ab') gives "abab7".
Numbers need to be converted to strings first, as in String(n).padStart(3, '0'). For formatting numbers with a minimum number of digits in a locale-aware way, Intl.NumberFormat with minimumIntegerDigits does the same job and also handles separators and other numbering systems.
Syntax
string.padStart(targetLength)
string.padStart(targetLength, padString)Parameters
- targetLengthThe length of the resulting string.
- padStringOptional. The text to pad with. Defaults to a space.
Return Value
The string padded at the start to the target length.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.padStart() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





