JavaScript String repeat() Tutorial

In this section, we will learn what the repeat() method is and how to use it in JavaScript.

JavaScript String repeat() Method

The repeat() method in JavaScript is used to repeat a text string in a number of times and return that new string as a result.

Note: using this method won’t change the original string value.

JavaScript String repeat() Syntax

string.repeat(count)

String repeat() Method Parameters:

The method takes one argument, and that is the number of times in which we want to repeat the target string value.

String repeat() Method Return Value:

The return value of this method is a new string that was repeated for any number of times we’ve set in its argument.

Example: using String repeat() method in JavaScript

const name = "John Doe";

const rep = name.repeat(10);

console.log(rep);

Output:

John DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn DoeJohn Doe

As you can see, the value of the `name` identifier is repeated 10 times, and the result is passed to the `rep` identifier.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies