+ 7
Next weird thing with arrays in js. Why?
Why 1 is in this array, but 9 isnt? Will it ever be possible to know everything about javascript? https://code.sololearn.com/WFo2gmHMaGFe/?ref=app
4 Answers
+ 7
arr.includes(9) //true
+ 4
in is used to check whether a key is present in the object or not.
At the implementation level, JavaScript stores array as an object with index as property name and element as value.
e.g.) var a = [9, 8, 7]
At the implementation level the above is stored as follows
var a = {"0": 9, "1":8, "2":7}
So,
console.log(0 in a); gives true because key 0 is present
+ 3
It says false because it doesn't check the numbers from array, but the indexes. Try to change the 9 in 3 and see what happens. Because 3 is the index of the last element. And that's why 1 returns true even it's not in the array.
+ 2
Because 1 in the index not include like
arr.index(9) //3
3 in arr // true
4 in arr // false