3 ms·
JavaScript object property keys are always strings, so technically it’s `arr[2]` that has the 2 coerced to a string.
by marcins 8y ago
JavaScript object property keys are always strings, so technically it’s `arr[2]` that has the 2 coerced to a string.
- Legogris 8y agoActually, that is incorrect. Arrays are different in JavaScript. I don't have time to look up the exact reference, but to get an idea: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... > Arrays cannot use strings as element indexes (as in an associative array) but must use integers. Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's object property collection. The array's object properties and list of array elements are separate, and the array's traversal and mutation operations cannot be applied to these named properties.
- Zalastax 8y agoIt's true that object keys are strings, but the array and object situation of JavaScript is quite confusing. Arrays are indexed by numbers, but still accept string indices, leading to behavior like this: https://stackoverflow.com/q/9526860 https://stackoverflow.com/q/9526860