Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.repeat

Repeats a string a given number of times.

Available since: ES2015

Syntax

str.repeat(count)

Parameters

ParameterTypeDescription
countnumberHow many times to repeat. Must not be negative.

Return value

stringA new string repeated `count` times.

Examples

Repeating a fragment:

console.log('ab'.repeat(3));

Output:

ababab

A separator line:

console.log('-'.repeat(20));

Output:

--------------------

Notes

When count is zero, an empty string is returned. A negative value throws an error.

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.