How many times have you found yourself printing strings like “————-” or “===========”? I do that a lot when I have to do console.logs or if I’m working on a console application or script. Adding dividers to the output makes it more readable.
Python has a very peculiar syntax for repeating strings, which consists in just multiplying the string by a number, like this:
-
"123"*4 // returns "123123123123"
We can achieve the same in Javascript using Array.join() like this:
-
Array(4).join("123"); // returns "123123123123"
-
Array(30).join("-"); // returns "——————————"
-
Array(2).join("<span>test</span>"); // returns "<span>test</span><span>test</span>"
Very useful for logs or to generate repeated html strings, etc.

New post: Little tricks: repeating strings in Javascript & Python http://bit.ly/ciRBWX
This comment was originally posted on Twitter