Aller au contenu principal

repeat()

repeat(str, count): string

Repeats a string a specified number of times.

DEPRECATED

Use string.repeat() directly instead.


Parameters

str: string | null | undefined

The string to repeat.

count: number | null | undefined

The number of times to repeat the string.


Returns: string

The repeated string.


See Also


Since

2.0.0


Also known as

repeat (Lodash, es-toolkit) · ❌ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Example

// ❌ Deprecated approach
repeat('hello', 3); // => 'hellohellohello'
repeat('-', 10); // => '----------'

// ✅ Recommended approach
'hello'.repeat(3); // => 'hellohellohello'
'-'.repeat(10); // => '----------'

How it works?

Repeats a string n times. Deprecated: Use string.repeat() directly (ES2015).

Native Equivalent

// ❌ repeat('ab', 3)
// ✅ 'ab'.repeat(3)

Use Cases

Repeat string 📌

Repeat string n times.

"ab".repeat(3);  // => "ababab"
"-".repeat(10); // => "----------"

Create separator

Build visual separators.

const divider = "=".repeat(40);
console.log(divider);

Indent code

Create indentation.

const indent = "  ".repeat(level);
const line = indent + content;