Repeating a string n number of times in JavaScript
In this post we will give you information about Repeating a string n number of times in JavaScript. Hear we will give you detail about Repeating a string n number of times in JavaScriptAnd how to use it also give you demo for it if it is necessary.
Learn, how to repeat a string in JavaScript.
To repeat a string in a specified number of times, we can use the built-in repeat() method in JavaScript.
Here is an example that repeats the following string for 4 times:
const name = "king";const repeat = name.repeat(4);console.log(repeat);
Output:
"kingkingkingking"
Similarly, we can also create our repeat() function like this:
function repeat(str, n) { if (!str || !n) { return; } let final = ""; while (n) { final += s; n--; } return final;}console.log(repeat("sai", 3)); // "saisaisai"
In the above function, we have used the while loop to concatenate the string for n number of times.
Hope this code and post will helped you for implement Repeating a string n number of times in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs