3 ms·
>> "x is an Array (which should only accept numerics for keys). E.g. you cannot have a String be a "key" in an Array." False. You can have anything you want in
by axod 17y ago
>> "x is an Array (which should only accept numerics for keys). E.g. you cannot have a String be a "key" in an Array."
False. You can have anything you want in an Array, since it's just an object.
var arr = [];
arr["hello"] = 57;
arr.world = function() {};
arr[999.99] = "flibble!";
arr[54] = 9;
// arr.length now equals 55
May or may not be a good coding style, but that's a separate matter.
- invisible 17y agoAgreed, just in a rush to comment before. I'd go one further and say using an Array that way is just plain worrisome in JavaScript. It just breaks the conceptual usage "sort of" of Array vs Object (for/while vs for each...in). That's why a lot of frameworks add an "each" iterator for Array/Hashes (Hashes being a "controlled" Object added by most frameworks).