CKTIS / JavaScript
String.prototype.padStart — padding text on the left
String.prototype.padStart pads the beginning of a string to a specified length by repeating and, when necessary, truncating the padding. It leaves the original unchanged and never shortens a string that is already long enough. Length is measured in UTF-16 code units.
Version: Introduced in ECMAScript 2017 (ES2017) as a standard-library method. Node.js 24 support does not establish browser support.
Syntax
str.padStart(maxLength[, fillString])String.prototype.padStart returns a string padded on the left. The maxLength argument specifies the target length of the entire result, not the number of characters to add. The original string remains unchanged. The method is useful for identifiers with leading zeros, for example; convert numbers to strings first.
The optional fillString is repeated to produce the required padding length. Excess is removed from the end of the padding, not from the original string. Omitting fillString or passing undefined uses a single U+0020 space. An empty filler "" leaves the string unchanged. If maxLength does not exceed the string's length, the method returns the string without padding or truncation.
maxLength undergoes ToLength conversion: positive fractions are truncated to integers, while negative values and NaN become zero. Length means UTF-16 code units, not visible characters or display width. Truncating the padding can split a surrogate pair, so simple ASCII fillers are appropriate for such identifiers. Calling the method with null or undefined as this throws a TypeError.
The method belongs to ECMAScript 2017 (ES2017), not Web APIs. The language specification does not establish which browser versions support it; check typeof String.prototype.padStart === "function" in the target environment. Having Node 24 in a repository does not establish browser support.
Examples
const id = "42";
console.log(id.padStart(5, "0"));
console.log(id.padStart(7, "ab"));
console.log(JSON.stringify(id.padStart(4)));
console.log(id.padStart(1, "0"));
console.log(id.padStart(5, ""));
console.log(id);Result
00042 ababa42 " 42" 42 42 42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.