JavaScript String padStart() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Instance method

What You’ll Learn

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

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.

This page is part of JavaScript String Methods. Related topics include padEnd() and length.

Understanding the padStart() Method

str.padStart(targetLength, padString) builds a new string whose length is at least targetLength, by inserting filler before str.

  • Padding is applied at the start of the string.
  • Default padString is " " (one space).
  • If padString is too long for the remaining slots, it is truncated.
  • If targetLength <= str.length, the original contents are returned as-is.
  • An empty padString cannot fill space — the original string is returned.

📝 Syntax

General forms of String.prototype.padStart:

JavaScript
str.padStart(targetLength)
str.padStart(targetLength, padString)

Parameters

  • targetLength — desired length of the result. If less than or equal to str.length, str is returned unchanged.
  • padString (optional) — string used to fill the start. Truncated if longer than needed. Default: " ".

Return value

A string of length targetLength (or the original length when no padding is needed):

SituationResult
Needs paddingRepeated/truncated padString + str
targetLength <= str.lengthOriginal string (no pad)
Omitted padStringSpaces fill the start
Long padStringTruncated to fit exactly
Empty padStringOriginal string (cannot pad)

Common patterns

JavaScript
"abc".padStart(10);            // "       abc"
"abc".padStart(10, "foo");     // "foofoofabc"
"abc".padStart(6, "123465");   // "123abc"
"abc".padStart(8, "0");        // "00000abc"
"abc".padStart(1);             // "abc"

"5".padStart(2, "0");          // "05"
String(9).padStart(3, "0");    // "009"

⚡ Quick Reference

GoalCode
Pad with spacesstr.padStart(n)
Zero-pad a numberString(n).padStart(width, "0")
Pad with custom fillerstr.padStart(n, "*")
Pad on the right insteadstr.padEnd(n, ".")
Check current widthstr.length

🔍 At a Glance

Four facts to remember about String.padStart().

Side
start / left

Filler goes before text

Default
" "

Space character

Too short target
no change

Returns original text

Mutates
no

Strings stay immutable

📋 padStart() vs padEnd() vs manual concat

padStart()padEnd()Manual + / repeat
Where padding goesBefore the textAfter the textWherever you place it
Best forZero-padded IDs, times, masksRight leaders, left-aligned labelsCustom layouts
Truncates fillerYesYesYou must handle it
ReadabilityClear intentClear intentEasy to get wrong

Examples Gallery

Examples follow MDN String.padStart() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Zero-pad a digit and explore common fillers.

Example 1 — Basic padStart()

MDN demo: turn "5" into a two-digit string with a leading zero.

JavaScript
const str = "5";

str.padStart(2, "0");
// "05"

"200".padStart(5);
// "  200"
Try It Yourself

How It Works

"5" is one character, so one "0" is inserted on the left to reach length 2. For "200", two spaces fill the start to length 5.

Example 2 — Spaces, Repeat, Zeros, and No-Op

MDN walkthrough of common padStart results.

JavaScript
"abc".padStart(10);          // "       abc"
"abc".padStart(10, "foo");   // "foofoofabc"
"abc".padStart(6, "123465"); // "123abc"
"abc".padStart(8, "0");      // "00000abc"
"abc".padStart(1);           // "abc"
Try It Yourself

How It Works

"foo" is repeated until length 10, then the original "abc" is appended. With targetLength 1, the string is already longer, so nothing is added.

📈 Practical Patterns

Zero-padding helpers, masking, and edge cases.

Example 3 — Fixed-Width Number Padding

MDN-style helper: left-fill a number with zeros (like printf "%0*d").

JavaScript
function leftFillNum(num, targetLength) {
  return num.toString().padStart(targetLength, "0");
}

leftFillNum(123, 5);   // "00123"
leftFillNum(7, 2);     // "07"
leftFillNum(42, 2);    // "42"

// Clock-style hours / minutes
const h = 9;
const m = 5;
`${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
// "09:05"
Try It Yourself

How It Works

Numbers must become strings first (toString() or String()). Then padStart inserts "0" characters until the width is reached.

Example 4 — Mask a Card Number

MDN demo: keep the last four digits and fill the rest with *.

JavaScript
const fullNumber = "2034399002125581";
const last4Digits = fullNumber.slice(-4);
const maskedNumber = last4Digits.padStart(fullNumber.length, "*");

maskedNumber;
// "************5581"
Try It Yourself

How It Works

slice(-4) keeps "5581". Padding that short string up to the original length with "*" produces a masked display of the same width.

Example 5 — No Pad, Empty Filler, and Length

Three edge cases beginners should know.

JavaScript
"ready".padStart(5, "0");  // "ready"  (already long enough)
"ready".padStart(3, "0");  // "ready"

"x".padStart(8, "");       // "x"      (empty filler cannot pad)

const padded = "Go".padStart(6, "*");
padded.length;             // 6
padded;                    // "****Go"
Try It Yourself

How It Works

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).

🚀 Common Use Cases

  • Zero-padded IDsString(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.

📝 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.

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.

Universal Widely available
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

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.

Continue with padEnd(), length, or the String methods hub.

💡 Best Practices

✅ Do

  • Use padStart() for left-side / leading padding
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about String.padStart()

Left-side padding to a target length.

5
Core concepts
🔎 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.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful