The repeat()
method in JavaScript is used to repeat a string a specified number of times. This method takes a single argument, which is the number of times to repeat the string. If the number of times to repeat the string is a positive integer, the repeat()
method returns a new string that is the result of repeating the original string the specified number of times. If the number of times to repeat the string is not a positive integer, the repeat()
method returns the original string.
Here is an example of how the repeat()
method works:
const string1 = 'Hello, world!';
const result = string1.repeat(3);
// the result will be: 'Hello, world!Hello, world!Hello, world!'
In the code example above, we first declare a string called string1
that contains the text Hello, world!
. We then use the repeat()
method to repeat the string1
string three times. The repeat()
method takes a single argument, which is the number of times to repeat the string. In this case, we pass in the number 3
as the argument to the repeat()
method.
As a result of calling the repeat()
method, the result
variable will be set to 'Hello, world!Hello, world!Hello, world!'
, which is the string1
string repeated three times. If the repeat()
method was called with a non-positive integer argument, the result
variable would be set to the original string1
string.
Leave a Reply