JavaScript string repeat method

JavaScript string repeat method:

To repeat a string for a specific number of times, JavaScript provides one method called repeat.

In this post, I will show you how to use repeat with an example.

Syntax of string.repeat:

repeat method is defined as below:


Here, count is an optional value. It is the number to repeat the string. If we don’t provide this value, it will return an empty string. Else, it will create a new string by appending the same string to the end and return that string.

Return value:

This method doesn’t change the original string. It creates one new string by repeating the original string count number of times.

For count equal to 0 or if we don’t provide the count value, it will return an empty string.

For a negative count value, it throws one RangeError.

Example program:

Let’s take a look at the below program:

const givenStr = 'hello';


It will print the below output:


For the first two, it printed empty strings.

JavaScript string replace

You might also like: