How to use the repeat() method In Javascript
This method is used to create a new string by repeating an existing string a specified number of times using the repeat() method. If a user provides the count to be 0 or negative, then an empty string is returned.
Syntax:
str.repeat(count);
// Here str is your string
// And count is how many times you want to repeat the string
Example: This example shows the implementation of the above-explained approach.
const str = "Gfg ";
const repeatedStr = str.repeat(3);
console.log(repeatedStr);
Output
Gfg Gfg Gfg
How to Repeat a String in JavaScript ?
The javascript string is an object that represents a sequence of characters. We are going to learn how can we repeat a string using JavaScript.
Below are the methods to repeat a string in JavaScript:
Table of Content
- Using the repeat() method
- Using a Loop
- Using the Array constructor and join() method
Contact Us