00:34 Many of the browser makers wrote non-standard features into the language and then if they grew popular, they got pulled into the standard later. It wasn’t really until ES6 that things started to settle down. This organic growth combined with the language’s simple, original intent has made for some odd language decisions.
Array objects don’t behave like lists or tuples in Python or like arrays in any other C-based language for that matter.
They’re closer to the idea of an ordered dictionary where the key is the index number of the array. If you delete an item from an array, it removes the item but doesn’t remove the space it was in. To do what you’d expect, you have to use the
.splice() method instead of the
It will tell you if there’s an item at that index in the array. If you use it like you do in Python you might even get back
True if it happens that the number in the array you’re checking for is a valid index.
As I mentioned before, by default, sorting only works on strings. Each of these numbers is typecast to a string and sorted based on that. Values starting with
1 come before values starting with
2, et cetera.
The comparison function is called with two items to be compared. If the first item is bigger than the second, return a positive value. If they’re the same, return
0. And if it is smaller, return a negative value. For numbers, subtracting one from the other will achieve this result. Finally, here’s that
in keyword that gets me all the time.
3 in is returning
False because there isn’t anything at index
If I choose the wrong example to test, it looks like
in does what I want to, and then I’m bug hunting. Hopefully, by writing this lesson and explaining it to you, I’ll finally remember to stop making this mistake.
Become a Member to join the conversation.