Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Under the hood, adding a string to an array invokes type coercion -- the Array's toString() method is called, which in turn calls join()

join() is specified not to check for key presence, and returns the empty string if the value is undefined

https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.join

> Array(3).join(',')
',,'

Splitting ',,' gives an array of empty strings (different from an array which has no keys at all)

> ',,'.split(',')
[ '', '', '' ]

You can then map over the array of empty strings, and the map function will be invoked for each empty string.

Conversely, map() is specified to check for key presence, and skip calling the map function if key isn't present.

https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.