To repeat string in JavaScript, you can use the repeat()
string method.
Consider this string,
// string
const str = "hehe";
Let's repeat this string 5
times using the repeat()
string method.
// string
const str = "hehe";
// repeat string 5 times
const repeatStr = str.repeat(5);
console.log(repeatStr); // "hehehehehehehehehehe"
See this example live in JSBin.
-
You can pass a positive integer as the number of times you want the string to be repeated as the first argument to the
repeat()
method. -
The method returns a new string.