The Wayback Machine - https://web.archive.org/web/20230201201027/https://www.geeksforgeeks.org/javascript-string-repeat/
Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript string.repeat() Method

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 04 Jan, 2023
Improve Article
Save Article

The string.repeat() is an inbuilt function in JavaScript that 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);

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
Related Articles

Start Your Coding Journey Now!