String.prototype.repeat() builds a new string by concatenating the original text a given number of times (same API as MDN String.prototype.repeat()). Learn valid counts, RangeError cases, float truncation, separators and fillers, five examples, and try-it labs.
01
Kind
Instance method
02
Returns
New string
03
count
0 … finite
04
Invalid
RangeError
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Need the same text several times in a row — a divider line, a laugh ("ha" repeated, or a template phrase? repeat() concatenates copies of the string and returns one new result.
Pass how many copies you want as count. Zero copies give an empty string. Negative values or Infinity throw RangeError. Decimals are truncated to integers.
💡
Beginner tip
Think of str.repeat(n) as str + str + … (n times) — without writing a loop.
str.repeat(count) returns a new string that contains str concatenated with itself count times.
count should be a non-negative finite number.
repeat(0) returns "".
Floats are truncated (e.g. 3.9 → 3).
Negative or infinite count throws RangeError.
The original string is never modified.
Foundation
📝 Syntax
General form of String.prototype.repeat:
JavaScript
str.repeat(count)
Parameters
count — number of copies. Must be between 0 and a finite value that does not overflow maximum string length. Converted to an integer (truncated toward zero).
Validate count before calling when it comes from user input. repeat is generic — it uses this converted to a string.
Applications
🚀 Common Use Cases
Divider / ruler lines — "-".repeat(40) in console or reports.
Indentation — " ".repeat(level * 2) for tree-style logs.
Decorative banners — stars or equals around a title.
Template phrases — repeat a unit inside larger strings.
Not for fixed width — prefer padStart / padEnd when the goal is a target length.
Not for huge counts — very large count can throw or exhaust memory.
🧠 How repeat() Builds the Result
1
Read count
Convert count to an integer (truncate toward zero).
Input
2
Validate range
Reject negatives and values that overflow string limits with RangeError.
Guard
3
Concatenate copies
Join the string with itself count times (or return "" for 0).
Build
4
✅
Return a new string
The original str is unchanged.
Important
📝 Notes
repeat(0) returns "" — it does not throw.
Negative count or Infinity throws RangeError.
Decimals are truncated toward zero before repeating.
repeat does not insert separators between copies.
Very large counts can throw if the result would be too long.
The method is generic — it can run on other objects that coerce to strings.
Compatibility
Browser & Runtime Support
String.prototype.repeat() is Baseline Widely available — supported across modern browsers since September 2015.
✓ Baseline · Widely available
String.repeat()
Safe for production. Prefer repeat() for copying text; use padStart/padEnd when you need a target length.
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
repeat()Excellent
Bottom line: Use String.repeat() to concatenate copies of a string. Guard against negative or infinite counts, and remember floats are truncated.
Wrap Up
Conclusion
String.prototype.repeat() is the clear way to build a string from repeated copies — dividers, indentation, and template phrases without a loop. Validate count, and reach for padStart / padEnd when your goal is a fixed width instead.
Use repeat() when you need n copies of the same text
Validate user-provided count before calling
Use Array(n).fill(str).join(sep) when you need separators
Prefer padStart / padEnd for fixed-width padding
Keep counts reasonable for memory and performance
❌ Don’t
Pass negative numbers or Infinity without a try/catch
Expect spaces between copies from repeat alone
Use repeat when you really need a target length
Assume decimals round up — they truncate
Mutate strings — always capture the returned value
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.repeat()
Concatenate copies of a string n times.
5
Core concepts
📝01
Returns
new string
API
🔎02
count 0
empty ""
Rule
❌03
Bad count
RangeError
Guard
⚡04
Floats
truncated
Coerce
📄05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.repeat(count) returns a new string made of the original text concatenated count times. For example "ha".repeat(3) returns "hahaha".
repeat(0) returns an empty string "". That is valid and does not throw.
A negative count, or a count that is Infinity (or otherwise too large for a string), throws RangeError.
count is converted to an integer (truncated toward zero), so "abc".repeat(3.5) behaves like "abc".repeat(3).
No. Strings are immutable. repeat() returns a new string.
repeat copies the whole string n times. padStart/padEnd grow a string to a target length by adding a filler on one side — they do not mean “copy the string n times.”
Did you know?
Before repeat() (ES2015), a common trick was new Array(n + 1).join(str) — clever, but easy to off-by-one. str.repeat(n) makes the intent obvious.