+ 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

28th Apr 2019, 4:04 PM
Sławek J.
4 Antworten
+ 7
arr.includes(9) //true
28th Apr 2019, 4:37 PM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 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
28th Apr 2019, 5:43 PM
Rishi Anand
Rishi Anand - avatar
+ 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.
28th Apr 2019, 4:16 PM
Charlie S
Charlie S - avatar
+ 2
Because 1 in the index not include like arr.index(9) //3 3 in arr // true 4 in arr // false
6th May 2019, 9:31 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar