JavaScript repeat() | Repeat String n Times

The JavaScript repeat() method is used when we need to repeat a string for n number of times. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

   <script>
      let myString = "fresherearth.com";
      
      let res = myString.repeat(3);
      document.getElementById("xyz").innerHTML = res;
   </script>
   
</body>
</html>
Output

JavaScript repeat() Syntax

The syntax of repeat() method in JavaScript is:

string.repeat(n)

The n argument is required, refers to a number that is used to create number of copies of specified string..

JavaScript Online Test


« Previous Tutorial Next Tutorial »