Lodash _.repeat() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
String utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.repeat() confidently in real string workflows.

01

Core Syntax

Call _.repeat(string, n).

02

Borders

Generate dash or star lines.

03

Templates

Repeat HTML fragments.

04

Edge cases

n=0 and empty strings.

05

Validation

Guard user-supplied counts.

06

Native repeat

ES2015 built-in alternative.

What Is _.repeat()?

_.repeat() returns a new string by repeating the input n times. It is ideal for generating visual separators, placeholder patterns, and repeated markup fragments without manual loops.

💡
Beginner tip

Think of _.repeat('-', 20) as a quick console divider line—twenty dashes in one expression.

📝 Syntax

javascript
_.repeat(string, [n=1])

Syntax Rules

  • string — The string to repeat.
  • n — Repeat count (non-negative integer; default 1).
  • Return value — Concatenated result string.
  • n = 0 — Returns empty string.
  • Empty input — Repeating empty string returns empty string.
javascript
import repeat from "lodash/repeat";

const border = repeat("-", 40);
// -> "----------------------------------------"

⚡ Quick Reference

TaskCode patternResult
Basic_.repeat('ab', 3)'ababab'
Border_.repeat('-', 20)Dash line
Zero times_.repeat('x', 0)''
Default n=1_.repeat('hi')'hi'
Template_.repeat('
  • \n', 5)
    List markup
    Native alt'-'.repeat(20)ES2015 built-in
    Mutates?
    No

    Returns new string

    n = 0
    Empty

    Zero repetitions

    n negative
    Empty

    Treated as zero

    Native
    repeat()

    Built-in equivalent

    🧰 Parameters

    string Required

    Substring to repeat.

    _.repeat('*', 5)
    n Optional

    Times to repeat (default 1).

    _.repeat('ab', 3)
    return value New string

    Concatenated repetitions.

    const line = _.repeat('-', 40)
    edge cases Note

    n=0 or empty string yields ''.

    _.repeat('', 100) -> ''

    Examples Gallery

    Practical _.repeat() patterns with copy-ready code and interactive Try It Yourself labs.

    📚 Getting Started

    Repeat a greeting with trailing space.

    Example 1 — Basic string repetition

    Repeat Hello three times.

    javascript
    const repeated = _.repeat("Hello ", 3);
    console.log(repeated);
    // -> "Hello Hello Hello "
    Try It Yourself

    How It Works

    The string is concatenated n times.

    📈 Practical Patterns

    Visual separators and formatted output.

    Example 2 — Generate a border line

    Twenty dashes for a console divider.

    javascript
    const border = _.repeat("-", 20);
    console.log(border);
    // -> "--------------------"
    Try It Yourself

    How It Works

    Single-character repeat is the most common border pattern.

    Example 3 — Repeated list items

    Build multiple <li> elements.

    javascript
    const items = _.repeat("<li>Item</li>\n", 3);
    console.log("<ul>\n" + items + "</ul>");
    Try It Yourself

    How It Works

    Repeat markup snippets before wrapping in a container.

    Example 4 — Zero repetitions

    n=0 always returns empty string.

    javascript
    console.log(_.repeat("Hello", 0));
    // -> ""

    How It Works

    Useful when a count-driven pattern should produce nothing.

    Example 5 — Validate before repeating

    Guard against invalid repeat counts.

    javascript
    function safeRepeat(str, n) {
      if (!Number.isInteger(n) || n < 0) return "";
      return _.repeat(str, n);
    }
    console.log(safeRepeat("x", 5));

    How It Works

    Wrap lodash when user input controls the count.

    🚀 Beyond the Basics

    Native repeat() alternative.

    Example 6 — Native String.repeat()

    ES2015 built-in repetition.

    javascript
    const str = "-";
    const native = str.repeat(20);
    const lodash = _.repeat(str, 20);
    // both -> "--------------------"

    How It Works

    Native repeat() is fine when lodash is not in the bundle.

    📋 Related string operations

    Topic_.repeatString.repeat()Array.joinLoop
    InputString + countString + countArray fillfor loop
    ReadabilityHighHighMediumLow
    n = 0''''''''
    MutatesNoNoNoNo
    Best forQuick patternsModern nativeDynamic arraysComplex logic

    🧠 How _.repeat() Works

    1

    Receive string

    Input string and repeat count.

    Input
    2

    Validate n

    Negative or zero n yields empty or single copy per lodash rules.

    Validate
    3

    Concatenate

    Join n copies of the string.

    Build
    =

    Return result

    New concatenated string.

    Done

    📝 Notes

    • _.repeat() returns an empty string when n is 0.
    • Repeating an empty string always returns ''.
    • For very long repetitions, native String.repeat() may be faster.
    • Validate n is a non-negative integer for user input.
    • Strings are immutable.
    • Native '-'.repeat(20) is equivalent in ES2015+.

    Conclusion

    _.repeat() builds repeated string patterns in one call—perfect for borders, templates, and visual separators. Validate the count for user input and prefer native repeat() when lodash is not already loaded.

    💡 Best Practices

    ✅ Do

    • Assign the return value—strings are immutable
    • Specify radix explicitly when parsing user input
    • Use RegExp /g flag for global replacements
    • Validate counts and inputs before transforming
    • Prefer native methods when lodash is not already imported

    ❌ Don’t

    • Expect the original string variable to change in place
    • Forget radix when using _.parseInt on user data
    • Use string patterns when you need all matches replaced
    • Import all of Lodash for a single string call
    • Skip NaN checks after parsing

    Key Takeaways

    01

    Repeat n

    Concatenate n copies.

    Basics
    02

    Borders

    Dash/star lines.

    Pattern
    03

    n = 0

    Empty result.

    Edge case
    04

    Templates

    Repeated markup.

    Use case
    05

    Native

    str.repeat(n).

    Alternative

    ❓ Frequently Asked Questions

    Returns a new string consisting of the input repeated n times.
    Returns an empty string.
    No. Strings are immutable.
    Yes: _.repeat('ab', 3) -> 'ababab'.
    String.prototype.repeat() since ES2015.
    Lodash treats invalid counts safely—result is empty string.
    Did you know?

    Lodash _.repeat and native String.repeat both return empty string when the count is zero—useful for disabling a pattern without branching.

    Practice _.repeat() in the Live Editor

    Open Try It, run the examples, and experiment with your own strings.

    Open Try It editor →

    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.

    6 people found this page helpful