String repeat in JavaScript

String repeat is a function that can be used to multiply a string for a given number of times, its a feature that is closely related to string multiplication operator in Python programming language, as compared to Python's syntax;

("String") * (number of times to repeat)
"A" * 3
// returns "AAA"

Something similar may be archived using string repeat function in JavaScript as;

("String").repeat(number Of times to repeat)

For example to triple "A" you can do as;

"A".repeat(3)

Practical example is with lyrics;

const part = "i feel you, "
const lyrics = part.repeat(4) + " A, B, C"
// "i feel you, i feel you, i feel you, i feel you, A, B, C"

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript