String.prototype.padStart() pads a string from the start until it reaches a target length (same API as MDN String.prototype.padStart()). Learn zero-padding, card masking, how padString repeats or truncates, when the original string is returned unchanged, how it compares to padEnd(), five examples, and try-it labs.
01
Kind
Instance method
02
Returns
New string
03
Pads
Left / start
04
Default pad
Space (" ")
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Need leading zeros on a number, a masked card display, or text shoved to the right in a fixed-width field? padStart() adds characters before your text until the result reaches targetLength.
The filler is optional. Omit it and JavaScript uses a single space. Provide a longer filler and it is repeated (and trimmed) to fill the gap. If the string is already long enough, nothing is added.
💡
Beginner tip
padStart = pad on the left. padEnd = pad on the right. Same length rules for both.
When no padding is possible or needed, you get the original text back. Successful pads always yield result.length === targetLength (measured in UTF-16 code units).
Applications
🚀 Common Use Cases
Zero-padded IDs — String(id).padStart(6, "0") for invoice or ticket numbers.
Clock / timer display — two-digit hours and minutes.
Masked values — show only the last few characters of a secret.
Right-aligned text columns — pad with spaces in monospace logs.
Not for visual width — emoji / wide glyphs may look uneven; length is code units.
Trailing padding — use padEnd() for leaders and left-aligned labels.
🧠 How padStart() Builds the Result
1
Read length + filler
Take targetLength and optional padString (default space).
Input
2
Compare lengths
If already long enough (or filler is empty), return the original text.
Guard
3
Fill the start
Repeat and/or truncate padString until the gap is filled.
Pad
4
✅
Return a new string
Filler first, original text last — str itself never changes.
Important
📝 Notes
padStart() pads on the left; padEnd() pads on the right.
Default filler is one space character.
If targetLength <= str.length, the string is returned unchanged.
Long fillers are truncated; short fillers are repeated.
Empty padString cannot add characters.
Length uses UTF-16 code units — same as str.length.
Compatibility
Browser & Runtime Support
String.prototype.padStart() is Baseline Widely available — supported across modern browsers since April 2017. Also available in Node.js, Deno, and Bun.
✓ Baseline · Widely available
String.padStart()
Safe for production. Use padStart for left-side padding; padEnd for right-side padding.
UniversalWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
padStart()Excellent
Bottom line: Use String.padStart() when you need a fixed width with filler on the left. Remember the default space, truncation rules, and convert numbers to strings before zero-padding. Internet Explorer needs a polyfill.
Wrap Up
Conclusion
String.prototype.padStart() is the clear way to grow a string to a target length by adding characters at the start. Reach for it for zero-padded IDs, clocks, and masks — and switch to padEnd() when the filler belongs on the right.
Convert numbers with String(n) before zero-padding
Use monospace fonts when aligning columns in the UI
Prefer padEnd() for trailing fillers and dotted leaders
Check str.length when debugging unexpected widths
❌ Don’t
Call padStart on a number without converting first
Expect padding when targetLength is too small
Pass an empty padString and expect filler
Assume length equals visual width for all Unicode
Mutate strings — always capture the returned value
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.padStart()
Left-side padding to a target length.
5
Core concepts
📝01
Side
start / left
API
🔎02
Default
space
Rule
🔢03
Zero-pad
String(n) + "0"
Pattern
⚡04
vs padEnd
left vs right
Compare
📄05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.padStart(targetLength, padString?) returns a new string padded at the start so its length reaches targetLength. The default padString is a single space.
If targetLength is less than or equal to str.length, padStart() returns the original string unchanged (as a new string with the same contents).
padString is truncated from the end so the result length equals targetLength. For example "abc".padStart(6, "123465") returns "123abc".
padStart adds padding before the text (on the left). padEnd adds padding after the text (on the right). Both use the same length and padString rules.
No. Strings are immutable. padStart() returns a new padded string.
Convert the number to a string first, then pad: String(num).padStart(width, "0") — for example (5).toString().padStart(2, "0") returns "05".
Did you know?
padStart and padEnd arrived together in ES2017. Zero-padding clocks and IDs used to need custom helpers; now String(n).padStart(2, "0") is the standard one-liner.