JavaScript String repeat() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.repeat(count) returns a new string made by concatenating the original text count times. It never mutates the original string.

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

Syntax

Call string.repeat. Start with this line.

JavaScript
str.repeat(count)
CallWhat it does
str.repeat(count)Primary signature.
string.repeat(...)Call with the arguments your case needs.

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

Library declarations
JavaScript
str.repeat(count)

Parameters

ParameterDescription
countRequired. How many copies; must be ≥ 0 and finite

Return Value

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

JavaScript Examples

Example 1 — Basic repeat()

Concatenate a phrase several times inside a sentence.

JavaScript
const mood = "Happy! ";

console.log(`I feel ${mood.repeat(3)}`);
// I feel Happy! Happy! Happy! 
Try It Yourself

How It Works

  1. Start with a unit. mood is "Happy! " (note the trailing space).
  2. Repeat three times. mood.repeat(3) concatenates that unit back-to-back.
  3. Embed in a template. The result lands after "I feel ".
  4. Original unchanged. mood is still a single "Happy! ".

Example 2 — Count Values (Including Floats)

0 yields empty; decimals truncate toward zero.

JavaScript
console.log(JSON.stringify("abc".repeat(0))); // ""
console.log("abc".repeat(1));                 // "abc"
console.log("abc".repeat(2));                 // "abcabc"
console.log("abc".repeat(3.5));               // "abcabcabc"
Try It Yourself

How It Works

  1. Zero copies. repeat(0) returns "" — valid, no throw.
  2. One or more. Each integer count adds that many full copies.
  3. Truncate floats. 3.5 becomes 3 before repeating.
  4. Use JSON for empty. JSON.stringify makes "" visible in the console.

Example 3 — Divider Lines and Indentation

Build separators and simple indent without a loop.

JavaScript
console.log("-".repeat(20));
// --------------------

const indent = "  ".repeat(2);
console.log(indent + "nested item");
//     nested item

console.log("*".repeat(5) + " STAR " + "*".repeat(5));
// ***** STAR *****
Try It Yourself

How It Works

  1. Single-character lines. "-".repeat(20) builds a divider of length 20.
  2. Indent with spaces. Two spaces repeated twice → four spaces of indent.
  3. Frame text. Stars on both sides wrap the label.
  4. Need separators? Use Array(n).fill(str).join(sep) — repeat itself does not insert gaps.

Browser & Runtime Support

String.prototype.repeat() is part of ES2015 and is Baseline Widely available across modern browsers.

✓ Baseline · ES2015

String.prototype.repeat()

Supported in Chrome 41+, Firefox 24+, Safari 9+, Edge 12+, and Node.js 4+. 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.repeat() Excellent

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

Frequently Asked Questions

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

Next: replace()

Learn how to replace the first match of a string or regular expression.

replace() 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