From the course: Learning ECMAScript 6+ (ES6+)

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using the string.repeat function

Using the string.repeat function - JavaScript Tutorial

From the course: Learning ECMAScript 6+ (ES6+)

Start my 1-month free trial

Using the string.repeat function

- [Instructor] We've looked at array and string methods like startsWith, endsWith, and includes. Well, I got another one for you, .repeat. The repeat method builds and returns a new string which contains a certain number of copies of the string. Let's take a look. First, we're going to create a variable, and our variable will be called yell. So, another woo yell for our class today. We're also going to add party. And party, we'll set equal to yell.repeat, and we'll pass in the number of times that we want this to repeat, which is 20. Now if we console.log party, we'll see woo, woo, woo, woo, woo, woo, woo 20 times. So the repeat function, again, is just going to make a copy of that string. You could also use the repeat function inside of an object that has a few methods. We're going to create an object called cat that has a meow method inside of it. Now this function is going to take in a certain number of times that a…

Contents