The Wayback Machine - http://web.archive.org/web/20221127111838/https://www.geeksforgeeks.org/javascript-string-repeat/amp/

JavaScript string.repeat() Method

Below is the example of the string.repeat() Method. 




<script>
    A = "forGeeks";
    a = A.repeat(2);
    document.write(a);
</script>

Output: 

forGeeksforGeeks

The string.repeat() is an inbuilt function in JavaScript which is used to build a new string containing a specified number of copies of the string on which this function has been called.

Syntax:  



string.repeat(count);

Return values: It returns a new string that contains the specified number of copies of the string.

JavaScript code to show the working of string.repeat() function: 

Code #1:  




<script>
 
    // Taking a string "gfg"
    A = "gfg";
 
    // Repeating the string multiple times
    a = A.repeat(5);
    document.write(a);
 
</script>

Output: 

gfggfggfggfggfg

Code #2: 




<script>
 
    // Taking a string "gfg"
    A = "gfg";
 
    // Repeating the string 2.9 times i.e, 2 times
    // because 2.9 converted into 2
    b = A.repeat(2.9);
    document.write(b + "<br>");
 
</script>

Output: 

gfggfg

Supported Browsers: 

 


Article Tags :