String.prototype.padStart
Pads the current string from the start with a given string until the resulting string reaches the given length
Syntax
string.padStart(targetLength, padString?)Parameters
| Parameter | Type | Description |
|---|---|---|
| targetLength | number | The length of the resulting string after padding |
| padString | string | The string to pad with. Defaults to space |
Return Value
A new string of the specified length with padding applied from the start
Examples
const str = '5';
console.log(str.padStart(3, '0')); // '005'const cardNumber = '4111111111111111';
const masked = cardNumber.slice(-4).padStart(16, '*');
console.log(masked); // '************1111'const hours = '9';
const minutes = '5';
console.log(`${hours.padStart(2, '0')}:${minutes.padStart(2, '0')}`); // '09:05'Understanding String.prototype.padStart
The String.prototype.padStart method in JavaScript pads the current string from the start with a given string until the resulting string reaches the given length. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.padStart(targetLength, padString?). It accepts 2 parameters: targetLength, padString. When called, it returns a new string of the specified length with padding applied from the start. Understanding when and how to use padStart() helps you write more expressive, readable code.
Common use cases for String.prototype.padStart include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-padend, string-repeat, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.padStart is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.