19

I can't find any information on this, but suppose I have

var arr = [1, 2, 3];
var x = arr[1.5];

I assumed that Javascript would floor the index and returns the item at index 1, but it seems at least in Chrome it just returns undefined.

Is this correct? I can't find any standard or documentation that confirms this. It's actually really inconvenient if so because I assumed the round-down behavior allows you to pass any float in the range [0, n) to an array index, but it seems you'll silently break your arrays if you do floating point math which isn't rounded.

Edit: if anyone's maintaining a list of javascript gotchas, please add this one. Now I have to review 10k lines of javascript code to see where this assumption I made is quietly causing bugs!

2

4 Answers 4

20

According to MDN: Array, array items are actually properties, and properties are identified by strings.

Accessing arr[1.5] is therefore the same as accessing arr['1.5'], and Firefox does actually do it that way.

This code:

var x = [0,1,2,3,4,5];

x[1.5] = 42;

alert(x);
alert(x['1.5']);
alert(x['1.50']);

outputs:

 0, 1, 2, 3, 4, 5
 42
 undefined

So, the item accessed using a floating point value is a property identified by the string representation of the number. Note that accessing an item using '1.50' is not the same as accessing an item using '1.5', so the index is not converted to a number.

I tested this in Internet Explorer and Chrome also, and they both do the same as Firefox.

7

I don't know why you think Chrome would "round" or "floor" the value when accessing an Array, but it doesn't do that at all. Neither does Firefox. It's just accessing a "key" which is not there and both browsers correctly return the undefined value (at least in the latest versions which I'm testing on).
Since Arrays are also Objects in ECMAscript, you're just accessing the value behind the keyname 1.5.

I really think I should point that out a little more. An Array is just an Object (it also inherits from Object) with a .length property and a few added methods. There is no numerical array-access like in C/C++. Its just accessing a keyname of an object.

var foo = [1,2,3];

foo instanceof Array; // true;
foo instanceof Object; // true;
3
  • There could still be optimizations for arrays in an implementation though (in whatever way). Jul 11, 2011 at 11:42
  • @FelixKling: of course. But that is not the question what happens under the hood. I just explained how Javascript behaves here.
    – jAndy
    Jul 11, 2011 at 11:44
  • 1
    I see, I guess coming from a C++ background I had assumed in my head that arr[f] implicitly converts f to an integer. Ouch. Jul 11, 2011 at 11:46
6

That's expected, since js converts the index into a string and accesses the object property (since object property indexes are always strings), so it'll actually look up arr['1.5']

For those non-believers, the relevant part of the standard is 15.4 which clearly defines array index as a string.

9
  • 2
    Is there really a rule in javascript that says "if the array index is a whole number, leave it as a number, but if it's not a whole number, convert it to a string"? Jul 11, 2011 at 11:27
  • 1
    @AshleysBrain: There is no real numerical array access in ecmascript. Those "numbers" are also just object keys as I pointed out in my answer.
    – jAndy
    Jul 11, 2011 at 11:30
  • 1
    @AshleyBrains. No, indexes are always converted to strings before lookup. For example a=[42]; a['0'] evaluates to 42.
    – davin
    Jul 11, 2011 at 11:31
  • 1
    @AshleyBrain: Actually they are all accessed as strings, so accessing arr[1] is the same as arr['1']. There is however some "magic" in length and toString, so that they only consider items that are whole numbers.
    – Guffa
    Jul 11, 2011 at 11:33
  • 1
    @AshleysBrain: I wouldn't say it like that. Again, Arrays are just Objects with a .length property and some special methods added.
    – jAndy
    Jul 11, 2011 at 11:35
0
var arr = [];
arr[0] = "hello";
alert(arr["0"]);
Output : hello

In JavaScript, whenever indexing an array element the index is converted by the JavaScript interpreter into a string which it then uses as a key. This is the same as using a key-value pair Hash Map in java.

arr[5-"5"]

In this case the mathematical expression is evaluated first and the result is converted to a string and used as the key. This is how it works:

<1> The interpreter converts "5" into an integer since "-" operator cannot be used with strings and then subtracts it from the left operand 5 to get the result 0.

<2> The result is then converted to string and used as the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.