Skip to content

Instantly share code, notes, and snippets.

@schneidmaster
Created January 4, 2019 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schneidmaster/8d65109a35977462f7c8b64498caa79d to your computer and use it in GitHub Desktop.
Save schneidmaster/8d65109a35977462f7c8b64498caa79d to your computer and use it in GitHub Desktop.

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