DEV Community

Discussion on: `at` coming soon to ECMAScript

Collapse
 
stegriff profile image
Ste Griffiths

The article covers this really:

Well, as it turns out, -1 is already a valid key. Arrays are really objects with indices as keys. So arr[-1] is looking at the arr object and the value of the "-1" key, which is undefined.

There can be a "-1" key present in the object, so the behaviour for "negative indices" is already defined by the language.

Collapse
 
jsnanigans profile image
Brendan Mullins

Why would you ever need negative indexes that have their own value? This sounds like a big in the language to me

 
stegriff profile image
Ste Griffiths • Edited

¯\(ツ)/¯ Any string is a valid key in a javascript object