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

JavaScript introduction to variables

Solutions to blocked URLs on Facebook

How to stop moz dotbot from accessing your website

How to set content disposition headers for express nodejs apps

How to set content disposition header for nginx server to force content download