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 math methods

How to build a twitter retweet bot using JavaScript

JavaScript default parameters

How to use 'console.assert' for assertion in JavaScript

HTML minifier module for Nodejs