Example 1 — Basic string repetition
Repeat Hello three times.
const repeated = _.repeat("Hello ", 3);
console.log(repeated);
// -> "Hello Hello Hello " How It Works
The string is concatenated n times.

By the end of this tutorial, you’ll use Lodash’s _.repeat() confidently in real string workflows.
Call _.repeat(string, n).
Generate dash or star lines.
Repeat HTML fragments.
n=0 and empty strings.
Guard user-supplied counts.
ES2015 built-in alternative.
_.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.
Think of _.repeat('-', 20) as a quick console divider line—twenty dashes in one expression.
_.repeat(string, [n=1]) 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.import repeat from "lodash/repeat";
const border = repeat("-", 40);
// -> "----------------------------------------" | Task | Code pattern | Result |
|---|---|---|
| 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 |
NoReturns new string
EmptyZero repetitions
EmptyTreated as zero
repeat()Built-in equivalent
string RequiredSubstring to repeat.
_.repeat('*', 5)n OptionalTimes to repeat (default 1).
_.repeat('ab', 3)return value New stringConcatenated repetitions.
const line = _.repeat('-', 40)edge cases Noten=0 or empty string yields ''.
_.repeat('', 100) -> ''Practical _.repeat() patterns with copy-ready code and interactive Try It Yourself labs.
Repeat a greeting with trailing space.
Repeat Hello three times.
const repeated = _.repeat("Hello ", 3);
console.log(repeated);
// -> "Hello Hello Hello " The string is concatenated n times.
Visual separators and formatted output.
Twenty dashes for a console divider.
const border = _.repeat("-", 20);
console.log(border);
// -> "--------------------" Single-character repeat is the most common border pattern.
Build multiple <li> elements.
const items = _.repeat("<li>Item</li>\n", 3);
console.log("<ul>\n" + items + "</ul>"); Repeat markup snippets before wrapping in a container.
n=0 always returns empty string.
console.log(_.repeat("Hello", 0));
// -> "" Useful when a count-driven pattern should produce nothing.
Guard against invalid repeat counts.
function safeRepeat(str, n) {
if (!Number.isInteger(n) || n < 0) return "";
return _.repeat(str, n);
}
console.log(safeRepeat("x", 5)); Wrap lodash when user input controls the count.
Native repeat() alternative.
ES2015 built-in repetition.
const str = "-";
const native = str.repeat(20);
const lodash = _.repeat(str, 20);
// both -> "--------------------" Native repeat() is fine when lodash is not in the bundle.
| Topic | _.repeat | String.repeat() | Array.join | Loop |
|---|---|---|---|---|
| Input | String + count | String + count | Array fill | for loop |
| Readability | High | High | Medium | Low |
| n = 0 | '' | '' | '' | '' |
| Mutates | No | No | No | No |
| Best for | Quick patterns | Modern native | Dynamic arrays | Complex logic |
_.repeat() WorksInput string and repeat count.
Negative or zero n yields empty or single copy per lodash rules.
Join n copies of the string.
New concatenated string.
n is 0.''.String.repeat() may be faster.n is a non-negative integer for user input.'-'.repeat(20) is equivalent in ES2015+._.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.
Concatenate n copies.
BasicsDash/star lines.
PatternEmpty result.
Edge caseRepeated markup.
Use casestr.repeat(n).
AlternativeLodash _.repeat and native String.repeat both return empty string when the count is zero—useful for disabling a pattern without branching.
Open Try It, run the examples, and experiment with your own strings.
6 people found this page helpful