Home >>JavaScript String Methods >JavaScript String repeat() method

JavaScript String repeat() method

JavaScript String repeat() method

JavaScript repeat() method is used to return a new string with a defined number of copies of the given input string it was called on.

Syntax:

string.repeat(count)

Parameter Values

Parameter Description
count This is a required parameter. It defines the number of times the original string value should be repeated in the new string.

Browser Support

Method Chrome Edge Firefox Safari Opera
repeat() 41.0 12.0 24.0 9 28.0

Here is an example of repeat() method:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "Jerry!!";
  document.getElementById("demo").innerHTML = str.repeat(5);
}
</script>
</body>
</html>
Output:

 

Click the button to see the Output.

 

Example 2:

 

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var str = "*/";
  document.getElementById("demo1").innerHTML = str.repeat(20);
}
</script>
</body>
</html>
Output:

 

Click the button to see the Output.

 

 


No Sidebar ads