I came across a rather peculiar implementation of a string repeat function for JavaScript.
There's a built-in for this since ES2015, and prior to that, the following was a common idom:
> str = "x"
> count = 3
> out = (new Array(count + 1).join(str)
But, an MDN contributor thought - we can optimize this! Can you figure out how and why it works?
A collegue of mine ran into this some interesting code behaviour today. I encountered this before, but didn't realize it was still a thing. Fun times!
Can you guess what caused the string to become an integer?
Dutch geek in London ♥️ free open-source software.
Principal software engineer at Wikimedia Foundation, QUnit lead maintainer. Interested in: Wikipedia, web performance, web standards, user-interface design, digital privacy and security. He/him.
Linguistics alt: @krinkle at polyglot.city.