repeat method
The repeat method creates a
new string containing the specified
number of copies of the original
string concatenated together.
Syntax
string.repeat(number);
Example
Let's specify the number 1
in the method parameter:
let res = 'abcde'.repeat(1);
console.log(res);
As a result of executing the code, our string will remain the same:
'abcde'
Example
Now we will copy the string
2 times:
let res = 'abcde'.repeat(2);
console.log(res);
The code execution result:
'abcdeabcde'
Example
Let's try to copy the string
-1 times:
let res = 'abcde'.repeat(-1);
console.log(res);
After execution we get an error:
'RangeError: Invalid count value: -1'
Example
If we specify the number
0 in the parameter:
let res = 'abcde'.repeat(0);
console.log(res);
As a result of executing the code, we will get an empty string:
''