Skip to content

How to create copies of a string in JavaScript?

To create copies of a string in JavaScript, we can use String.prototype.repeat() method. It takes a number as an argument and repeats the string n times and returns it.

let str = "poopcode";

console.log(str.repeat(2)); //poopcodepoopcode

console.log(str.repeat(5)); //poopcodepoopcodepoopcodepoopcodepoopcode
See also  Permuting Two Arrays - Hackerrank Challenge - JavaScript Solution

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.