Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. String .padStart() / padEnd() / repeat()

String .padStart() / padEnd() / repeat() Since: ES2017(ECMAScript 2017)

Pads the beginning or end of a string with characters until it reaches a specified length, or repeats a string a given number of times.

Syntax

// Pads the beginning of a string until it reaches the target length.
string.padStart(targetLength)
string.padStart(targetLength, "padString")

// Pads the end of a string until it reaches the target length.
string.padEnd(targetLength)
string.padEnd(targetLength, "padString")

// Repeats a string a specified number of times.
string.repeat(count)

Method List

MethodDescription
padStart(targetLength)Pads the beginning of the string with spaces until it reaches the target length.
padStart(targetLength, "padString")Pads the beginning of the string with the specified string. If the string is already at or beyond the target length, it is returned as-is.
padEnd(targetLength)Pads the end of the string with spaces until it reaches the target length.
padEnd(targetLength, "padString")Pads the end of the string with the specified string. If the string is already at or beyond the target length, it is returned as-is.
repeat(count)Returns a new string consisting of the original string repeated the specified number of times.

Sample Code

// Use padStart to zero-pad a number.
var num = "5";
console.log(num.padStart(3, "0")); // Outputs "005".

// Useful for formatting dates.
var month = "3";
var day = "7";
var date = "2026-" + month.padStart(2, "0") + "-" + day.padStart(2, "0");
console.log(date); // Outputs "2026-03-07".

// Use padEnd to left-align strings.
console.log("Name".padEnd(10, ".") + "Kuu");  // Outputs "Name......Kuu".
console.log("Price".padEnd(10, ".") + "500");  // Outputs "Price.....500".

// If the string is already at or beyond the target length, it is returned as-is.
console.log("Hello".padStart(3, "0")); // Outputs "Hello".

// Use repeat to repeat a string.
console.log("Ha".repeat(3)); // Outputs "HaHaHa".
console.log("-".repeat(20)); // Outputs "--------------------".

// Passing 0 to repeat returns an empty string.
console.log("abc".repeat(0)); // Outputs an empty string "".

Overview

padStart() and padEnd() pad the beginning or end of a string with characters until it reaches the specified length. If you omit the second argument, the string is padded with spaces. The most common use case is zero-padding numbers, which is handy for formatting dates and times.

repeat() returns a new string consisting of the original string repeated the specified number of times. It is useful whenever you need to repeat the same string multiple times, such as generating separator lines or building indentation strings. Passing 0 returns an empty string, while passing a negative number or Infinity throws an error.

All of these methods leave the original string unchanged and return a new string.

Browser Compatibility

Chrome Chrome
62+
56 or earlier ×
Firefox Firefox
57+
47 or earlier ×
Safari Safari
18+
9 or earlier ×
Edge Edge
80+
14 or earlier ×
IE IE
11 ×
Not supported in any version
Opera Opera
49+
43 or earlier ×
iOS Safari iOS Safari
18+
9 or earlier ×
Android Browser Android Browser
62+
56 or earlier ×
Chrome Android Chrome Android
62+
56 or earlier ×
Firefox Android Firefox Android
79+
47 or earlier ×

If you find any errors or copyright issues, please .