JavaScript | string.repeat()

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(a);

Parameters: Here the parameter ‘a’ is an integer value which show the number of times to repeat the given string. The range of the integer “a” is from zero (0) to infinity.
Return values: It returns a new string which contains the specified number of copies of the string.

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

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

gfggfggfggfggfg

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

gfggfg


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.