Syntax
str.padStart(targetLength, padString)
str.padEnd(targetLength, padString)
str.repeat(count)Examples
Padding Numbers
A common use case: formatting numbers with leading zeros.
console.log("5".padStart(2, "0")); // "05"
console.log("42".padStart(5, "0")); // "00042"
console.log("7".padEnd(3, "*")); // "7**"
// Formatting a countdown timer
function formatTime(minutes, seconds) {
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}
console.log(formatTime(3, 5)); // "03:05"repeat()
Repeating a string a specified number of times.
console.log("ab".repeat(3)); // "ababab"
console.log("-".repeat(20)); // "--------------------"
// Simple indentation helper
function indent(text, level) {
return " ".repeat(level) + text;
}
console.log(indent("nested item", 2)); // " nested item"Best practices
- Use padStart() for right-aligning content like numbers, and padEnd() for left-aligning content like table columns
- Convert numbers to strings first with String() before padding, since pad methods only work on strings
- Use repeat() for generating separator lines, indentation, or simple ASCII visualizations
- Watch out for negative or non-integer arguments to repeat() - they throw a RangeError
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
String Basics: length, charAt(), at()
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.