Lodash _.repeat() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
String

Definition

Repeat a string n times and return the concatenated result. Part of Lodash String (stable — not deprecated). If n is less than 1, the result is an empty string.

Pass the string and options shown below. Check the return type before you chain another helper.

Syntax

Call _.repeat. Start with this line.

Lodash
_.repeat([string=''], [n=1])
CallWhat it does
_.repeat([string=''], [n=1])Primary signature.
_.repeat(string)Transform or inspect a string.

Prefer import repeat from "lodash/repeat" when you want a smaller bundle.

Library declarations
Lodash
_.repeat([string=''], [n=1])

Parameters

ParameterDescription
stringOptional. Text to repeat (default '')
nOptional. Number of copies (default 1)

Return Value

Return ValueDescription
*New repeated string (or '')

Lodash Examples

Example 1 — Official: Repeat a Character

Build a short run of the same character.

Lodash
_.repeat("*", 3);
// → "***"
Try It Yourself

How It Works

  1. Source. Single character "*".
  2. Count. n = 3 copies.
  3. Join. Concatenates to "***".
  4. Docs. Matches the official first example.

Example 2 — Official: Repeat a Word

The whole string is copied, not each character separately.

Lodash
_.repeat("abc", 2);
// → "abcabc"
Try It Yourself

How It Works

  1. Unit. "abc" is one unit to copy.
  2. Twice. Two units → "abcabc".
  3. Not character-wise interleaving.
  4. Docs. Matches the official second example.

Example 3 — Official: n = 0

Zero (or less) yields an empty string.

Lodash
_.repeat("abc", 0);
// → ""
Try It Yourself

How It Works

  1. Guard. n < 1 means no copies.
  2. Empty. Returns "".
  3. Safe. Does not throw (unlike native negative repeat).
  4. Docs. Matches the official third example.

Example 4 — Build a Separator Line

Handy for logs, CLI output, and simple UI dividers.

Lodash
var line = _.repeat("-", 20);

console.log(line);
// → "--------------------"
Try It Yourself

How It Works

  1. Char. Use "-" as the unit.
  2. Width. n = 20 sets the line length.
  3. Reuse. Store in a variable for printing.
  4. Tip. Change the character for "=" or "*" borders.

Example 5 — repeat vs padStart

Copy count vs target width.

Lodash
console.log(_.repeat("0", 5));       // "00000"
console.log(_.padStart("42", 5, "0")); // "00042"
Try It Yourself

How It Works

  1. repeat. Five copies of "0" — no original payload.
  2. padStart. Keeps "42" and fills to width 5.
  3. When repeat. Separators, masks, decorative runs.
  4. When pad. Fixed-width IDs and aligned numbers.

Browser & Runtime Support

_.repeat is a Lodash String helper available in 4.x. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native String.prototype.repeat (ES2015) covers the same idea in current engines, with stricter error rules for invalid n.

✓ Stable · Lodash 4.x

_.repeat()

Available in [email protected] full builds and the lodash/repeat modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
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
_.repeat Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import repeat from "lodash/repeat" for smaller builds. Use native repeat in modern-only code if you prefer zero Lodash for this helper.

Frequently Asked Questions

_.repeat([string=''], [n=1]) returns string repeated n times. Official: _.repeat('*', 3) → '***' and _.repeat('abc', 2) → 'abcabc'.
Official: _.repeat('abc', 0) → ''. Lodash also treats values less than 1 as producing an empty string.
No. Strings are immutable. It always returns a new string.
_.repeat builds a string by copying the whole source n times. _.padStart grows a string to a target length by adding fill characters on the left.
Yes. String.prototype.repeat (ES2015). Native repeat throws RangeError for negative n or Infinity; Lodash returns '' for n < 1.
Use import repeat from "lodash/repeat" or require("lodash/repeat") for tree-shaking. Full build: import _ from "lodash" then _.repeat.

Next: _.replace()

Replace matches in a string with a string or function.

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

10 people found this page helpful