Hacker News new | ask | show | jobs
by cogman10 1845 days ago
Because in most other languages, an array is an enumeration of values. That's it.

In Javascript, arrays are actually dictionaries. But not full dictionaries, rather just dictionaries that can have either a string or numeric key.

It's messy because an array in javascript isn't just "an array" it's this mismesh of features that are unexpected to a new-to-javascript developer.

Unexpected is the enemy of readable code.

2 comments

No, arrays are just objects with a magic "length" property (technically: a special [[DefineOwnProperty]] internal method which sometimes also mutates "length"). Like objects, they only support strings as keys.

c.f. https://tc39.es/ecma262/#sec-array-exotic-objects

It’s only unexpected if you’re bringing in expectations from somewhere else. Coming from JS as basically my first language, it was surprising and a bit annoying that you couldn’t assign properties to hardly anything in other languages, even functions in languages where functions were supposedly first class.