DEV Community

Cover image for How to repeat strings in JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on Originally published at melvingeorge.me

How to repeat strings in JavaScript?

Originally posted here!

To repeat string in JavaScript, you can use the repeat() string method.

Consider this string,

// string
const str = "hehe";
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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.

Feel free to share if you found this useful 😃.


Top comments (0)