String repeat() Method - Javascript String

Javascript examples for String:repeat

Description

The repeat() method returns a new string with a specified number of copies.

Syntax

string.repeat(count);

Parameter Values

Parameter Description
count Required. The number of times the original string value should be repeated in the new string

Return Value:

A String, a new string containing copies of the original string

The following code shows how to Make a new string by copying a string twice:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*from   w ww  .j ava2 s  . c o  m*/
    var str = "Hello world!";
    document.getElementById("demo").innerHTML = str.repeat(2);
}
</script>

</body>
</html>

Related Tutorials