DEV Community

Discussion on: `at` coming soon to ECMAScript

Collapse
 
jsnanigans profile image
Brendan Mullins

Why don't they just add support for arr[-1]. Seems like the simplest solution

Collapse
 
laurieontech profile image
Laurie

I believe I explained that in the post. It’s already a valid key.

Collapse
 
jsnanigans profile image
Brendan Mullins

Sure, but does it make sense that -1 is a key? That actually sounds like a bug to me xD instead all negative numbers could be used as reverse indexes instead of looking for a value in the array where there really should not be any.

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