0

I'm wondering if I can rely on the fact that [1,,2] only has two keys: 0 and 2, or if anyone happens to know if any JS engines will also give me a 1.

Every browser I've tested shows keys 0, 2, but I don't have older versions available at the moment, or an android phone, or ...

Reasoning:

I'm writing a custom timer library on top of requestAnimationFrame and so am returning cancelable ids based on internal array indices. I'm trying to figure out if simply delete ary[ix] is sufficient to be able to walk all of the object keys on the array without extra sanity checks.

  • @MichaelBerkowski -- Yes, the length is `3`, but a `for-in` loop, or `Object.keys( ary )` only has keys of `0` and `2` –  May 03 '14 at 21:05

2 Answers2

2

You are covered if you are willing to assume that your code will be running on a conforming implementation of ECMAScript. From the spec:

Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array, that element does not contribute to the length of the Array.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Yes, the value is undefined, but it doesn't say whether or not the key is defined –  May 03 '14 at 21:06
  • 1
    @zyklus: "Elided array elements are not defined", especially in that context, means that the missing elements don't actually result in defined indexes. Otherwise it would have said that they are defined and their value is `undefined`, and skipped the whole "increases the index" part. – Jon May 03 '14 at 21:07
  • I'm not so sure about that interpretation. For instance, I could easily see a JS engine effectively doing `ary[1] = undefined`, which would define the key but leave the value undefined. –  May 03 '14 at 21:08
  • @zyklus: That would not leave the value undefined. It would define it to be `undefined`. Sorry for sounding like a pedant, the terminology doesn't help. The reason `ary[1]` evaluates to `undefined` is the same reason `ary[42]` does it: there is no key `42`, just as there is no key `1`. – Jon May 03 '14 at 21:10
  • Fair enough. I'm reading it the same way as you, fwiw, I'm just not certain I trust it based on the terminology. Though I suppose I could just finish implementing this and wait for a possible eventual bug :) –  May 03 '14 at 21:11
  • @zyklus: This is of course not a formal argument, but consider (Chrome console): `[1,,3]` => `[1, undefined × 1, 3]` while `[1,undefined,,3]` => `[1, undefined, undefined × 1, 3]`. If you try more commas you 'll see that `undefined x N` gets "special" treatment, to show you that the indexes are not actually there (`undefined x 1` vs just `undefined`). – Jon May 03 '14 at 21:14
  • Yeah, but that could be specific to the V8 array implementation. It's relatively new behavior to show `undefined x N` –  May 03 '14 at 21:15
  • Sure. That's why in the proper answer I just quoted the spec. – Jon May 03 '14 at 21:16
0

[1,,3] will produce [1, undefined, 3]

The length returned is 3.

Edit: to clarify, even though the value is present, a key is not returned.

Object.keys([1,,3]) returns ["0", "2"]


Edit 2: a great answer about checking if an array key exists.
Checking if a key exists in a JavaScript object?

Community
  • 1
  • 1
Syntax
  • 2,073
  • 15
  • 15
  • And `Object.keys( [1,,3] ) == [0,2]`, which is what my question actually is. –  May 03 '14 at 21:12
  • tested on latest FireFox and Chrome release. – Syntax May 03 '14 at 21:12
  • @Syntax: The value is *not* present. `a[1]` is `undefined` for exactly the same reason that `({})["this key does not exist"]` is `undefined`. – Jon May 03 '14 at 21:15
  • @Jon, okay value was a bad word choice. Nevertheless, the length is 3, value of index 1 is undefined, and the keys are 0 and 2. – Syntax May 03 '14 at 21:19
  • @Syntax -- Technically index 1 is "not defined", it doesn't have a value of `undefined`, which is the whole point. If you say `ary[1] = undefined`, your array will look the same, but you will now have a key of `1` as well –  May 03 '14 at 21:21
  • @zyklus, you are correct. `1 in a` returns false. I was mistaken in that part. – Syntax May 03 '14 at 21:45