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

What is 'this.' keyword in JavaScript

How to iterate array elements using 'for loop' in JavaScript