JavaScript(JS) string method - repeat

The repeat() method is a string method in JavaScript that is used to repeat a string a specified number of times.

Here is the syntax for the repeat() method:

str.repeat(count)
Sour‮ec‬:www.theitroad.com

Here, str is the string you want to repeat, and count is the number of times you want to repeat the string. The count parameter must be a positive integer.

The repeat() method returns a new string that is the concatenation of the original string repeated count number of times.

Here is an example of using the repeat() method:

let str = "hello";
let result = str.repeat(3);
console.log(result); // "hellohellohello"

In the example above, the repeat() method repeats the str three times. The resulting string is "hellohellohello".

If the count parameter is less than or equal to 0, the repeat() method returns an empty string.

Here is an example of using the repeat() method with a count of 0:

let str = "hello";
let result = str.repeat(0);
console.log(result); // ""

In the example above, the repeat() method returns an empty string since the count is 0.

Note that the repeat() method does not modify the original string. It returns a new string that is the result of repeating the original string.