Skip to content
Related Articles

Related Articles

Improve Article

JavaScript string.repeat() Method

  • Last Updated : 06 Oct, 2021
Geek Week

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

  • Example:

Javascript




<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);
  • count: count is an integer value which 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.



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

Code #1:  

Javascript




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

Output: 

gfggfggfggfggfg

Code #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);
    document.write(b + "<br>");
 
</script>

Output: 

gfggfg

Supported Browsers: 

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

 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :