JavaScript String padStart() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.padStart(targetLength, padString?) returns a new string padded at the start until it reaches targetLength. Default filler is a space. It never mutates the original.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.padStart. Start with this line.

JavaScript
str.padStart(targetLength, padString?)
CallWhat it does
str.padStart(targetLength, padString?)Primary signature.
string.padStart(...)Call with the arguments your case needs.

Built into JavaScript. Call padStart(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.padStart(targetLength, padString?)

Parameters

ParameterDescription
targetLengthRequired. Desired final .length (UTF-16 units)
padStringOptional. Filler; default " "; truncated if too long

Return Value

Return ValueDescription
*The value returned by string.padStart().

JavaScript Examples

Example 1 — Leading Zeros and Default Spaces

Pad on the left with "0" or the default space.

JavaScript
console.log("5".padStart(3, "0"));  // "005"
console.log("200".padStart(5));     // "  200"
Try It Yourself

How It Works

  1. Measure the gap. "5" has length 1; target is 3 → need 2 chars.
  2. Prepend the filler. Two "0" characters make "005".
  3. Default is space. Omitting padString pads "200" with two spaces on the left.
  4. New string only. The originals stay unchanged.

Example 2 — Repeat and Truncate the Filler

A short filler repeats; a long filler is cut to fit exactly.

JavaScript
console.log("abc".padStart(10, "foo"));   // "foofoofabc"
console.log("abc".padStart(6, "123456")); // "123abc"
console.log("abc".padStart(2, "."));      // "abc"
Try It Yourself

How It Works

  1. Repeat to fill. "foo" is tiled until seven chars are needed → "foofoof".
  2. Truncate excess. Only the first three digits of "123456" are kept → "123abc".
  3. No-op when short. Target 2 is already less than length 3 → "abc".
  4. Exact length. Successful pads always end with result.length === targetLength.

Example 3 — padStart() vs padEnd()

Left pad for zeros; right pad for labels.

JavaScript
console.log(String(9).padStart(2, "0") + ":" + String(5).padStart(2, "0")); // "09:05"
console.log("5".padStart(3, "0"));  // "005"
console.log("5".padEnd(3, "0"));    // "500"
Try It Yourself

How It Works

  1. Convert numbers first. String(9) becomes "9", then pads to "09".
  2. Clock-style width. Hours and minutes each get width 2 → "09:05".
  3. Same digit, opposite side. padStart → "005"; padEnd → "500".
  4. Rule of thumb. Leading zeros → padStart; right-side fill → padEnd.

Browser & Runtime Support

String.prototype.padStart() is part of ES2017 and is Baseline Widely available across modern browsers.

✓ Baseline · ES2017

String.prototype.padStart()

Supported in Chrome 57+, Firefox 48+, Safari 10+, Edge 15+, and Node.js 8+. Not available in Internet Explorer without a polyfill.

98% Modern browser support
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
String.padStart() Excellent

Bottom line: Safe for modern web apps and current Node LTS releases. Feature-detect with if ("padStart" in String.prototype) or use a small polyfill for very old targets.

Frequently Asked Questions

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 (same contents).
padString is truncated so the result length equals targetLength. For example "abc".padStart(6, "123456") 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 String(5).padStart(2, "0") returns "05".

Next: String.raw()

Learn how to keep backslashes raw in template literals.

String.raw() tutorial →

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.

11 people found this page helpful