Below is the example of the string.repeat() Method.
- Example:
<script>A ="forGeeks";a = A.repeat(2);document.write(a);</script>chevron_rightfilter_none - 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);
-
Parameters:
- 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:
<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 conterted into 2 b = A.repeat(2.9); document.write(b + "<br>"); </script> |
Output:
gfggfg
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
Recommended Posts:
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript dataView.getInt16() Method
- JavaScript | RegExp toString() Method
- JavaScript | clearTimeout() & clearInterval() Method
- JavaScript Math cosh() Method
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.


