The repeat() method in JavaScript is used to construct and return a new string which contains the specified number of copies of the string on which it was called.
Syntax: string.repeat(count)
- count: An integer representing the number of times to repeat the string. If this value is negative or zero, an empty string is returned.
Example:
let str = "Hello"; let repeatedStr = str.repeat(3); console.log(repeatedStr); // Output: "HelloHelloHello"
Explanation:
- We have a string str containing "Hello".
- We use repeat() to create a new string that contains three copies of the original string.
- The resulting string, "HelloHelloHello", is stored in the variable repeatedStr and logged to the console.
Code Compiler Link: https://app.singhteekam.in/codecompiler/