Skip to main content

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;