Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String repeat() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

Syntax:

string.repeat(count);

Parameters: This method accepts a single parameter.

  • count: count is an integer value that shows the number of times to repeat the given string. The range of the integer “count” is from zero (0) to infinity.

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

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

Example 1: In this example, we will repeat the string value two times using the string.repeat() method and print it in the console.

Javascript




<script>
    A = "forGeeks";
    a = A.repeat(2);
    console.log(a);
</script>


Output: 

forGeeksforGeeks

Example 2:  In this example, we will repeat the string value five times using the string.repeat() method and print it in the console.

Javascript




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


Output: 

gfggfggfggfggfg

Example 3: In this example, we will repeat the string value 2.9 times using the string.repeat() method, we will see that it only prints two times as 2.9 is converted to 2.

Javascript




<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);
    console.log(b);
</script>


Output: 

gfggfg

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browsers:

  • Google Chrome 41 and above
  • Edge 12 and above
  • Firefox 24 and above
  • Opera 28 and above
  • Safari 9 and above

My Personal Notes arrow_drop_up
Last Updated : 15 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials