DEV Community

AquaCat
AquaCat

Posted on

【JavaScript(TypeScript)】The repeat() method

The repeat() method returns a new string that consists of the specified number of copies of the string.

For example:

const catSound="meow";
console.log(Cat makes sounds "${catSound.repeat(5)}");
Enter fullscreen mode Exit fullscreen mode

The console shows as below:

'Cat makes sounds "meowmeowmeowmeowmeow"' //"meow" is repeated 5 times as specified in the brackets.
Enter fullscreen mode Exit fullscreen mode

If you specify the number "0"

'Cat makes sounds ""'
Enter fullscreen mode Exit fullscreen mode

If you specify a negative number (e.g. -1) or an infinity number (e.g. 1/0)
It returns "Range Error."

Top comments (0)