Repeat Strings in JavaScript

Learn how to repeat Strings in JavaScript. This video contains 2 methods to achieve the task. It shows how using while loop you can repeat a string multiple times. It then, shows a method that was introduced in ES6 on String constructor called repeat. With this method, you can reduce the code that you write from multiple lines to a single line.


// Pre ES6
let reminder = "Drink Water! "
let count = 0;
let result = '';
while (count < 10) {
result += reminder;
count += 1;
// ES6 Method
console.log("Drink Water! ".repeat(100))

Full Transcript

0:00 Hey, it's Harit Himanshu from bonsaiilabs. Welcome to this series on JavaScript. Today In this video, we will learn how to repeat strings in JavaScript. It's common that as developers we sit in front of computers all day long to create great products. In order to maximize our output, we supplement ourselves using caffeine such as a nice cup of latte. But as a side effect, our body tends to get dehydrated, leaving us tired and exhausted. We know that if we fill ourselves up with good amount of water on a schedule basis, we will have much more energy to perform better. So as programmer, our task now is to add a piece of code that repeats itself multiple times. This way we can run this code either in a Cron job or put inside the code base to log on the console. It will remind us to keep ourselves hydrated.

0:45 So how do we write a code that repeats a string over and over? Let's find out. One way to do that is to create a reminder text. Then create a variable called count, which will keep track of how many times we want to repeat a pattern. Next we will create a variable called result that will contain the reminder takes as many times as we want. Next we will create a while loop and we will iterate let's say 10 times and within this while loop we are going to add the reminder text to the result and then we will increment the count every single time so that we only run it for 10 times. And finally we print the result on the console and as we run it you would see that the reminder has been printed 10 times onto the console. Now as you see, we have written six lines of code

1:32 to repeat the pattern. But there is a better way to do that. In ES6 there is a method called as a repeat on strings where it takes the number as the argument. So if you say reminder.repeat 10 times and run it, we get exactly the same output in just a single line. Since it takes a number of, you might as well send a floating point number as the arguments such as 3.5 and if you run that we will see that the pattern is repeated only three times. So what repeat does, it takes the argument and rounds off to the nearest integer in this case. And what if we pass 0 as the argument. So when we send the arguments 0 to the repeat and run the code as expected, it will not print out anything on the console. If you pass in a negative number as the argument and then run it, you will see that it throws a range error.

2:20 It's because repeat expects the argument to be between zero and infinity where infinity is excluded. So if you try to perform an operation which leads to infinity such as one by zero, you would see that it throws the range error again. The most common use case would be to add the separator between your logs. You could do this easily using repeat by adding a star or any other character and repeating the pattern 20 times, let's say, and when you run, you would see that the star has been repeated 20 times and that's it. I hope you learned something new today. If you like the video, please hit the subscribe button on our channel to receive more engaging content about JavaScript every single week. Until then, keep learning new skills and apply them regularly to become an expert programmer. See you next time.