[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Repeat a string

Repeat a string

JavaScript

const repeat = (str, numberOfTimes) => str.repeat(numberOfTimes)

// Or
const repeat = (str, numberOfTimes) => Array(numberOfTimes + 1).join(str)

TypeScript

const repeat = (str: string, numberOfTimes: number): string => str.repeat(numberOfTimes)

// Or
const repeat = (str: string, numberOfTimes: number): string => Array(numberOfTimes + 1).join(str)