Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
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.
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.
Why don't they just add support for arr[-1]. Seems like the simplest solution
I believe I explained that in the post. It’s already a valid key.
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.
The article covers this really:
There can be a "-1" key present in the object, so the behaviour for "negative indices" is already defined by the language.
Why would you ever need negative indexes that have their own value? This sounds like a big in the language to me
¯\(ツ)/¯ Any string is a valid key in a javascript object