Message from JavaScript discussions

August 2020

— Ye, show me what they sell in *B* like some online-shop example.. that's not a trade but gambling

— 

First = 2 in [1, 2];
const second = '2' in [0, 1, 2];

console.log(first, second);


Output : false,true.


Can someone,please explain.

— In array in checks for indexes and in object it checks for keys

— Coz,its checking for an index 2 in the first which isn't there so it returns false ?

Second case ? Why it's returning true as output ?

Message permanent page

— Yes

— Second case there are 3 elements in the array hence index 2 exists

— But '2' is a string,right ?

Aren't we checking for a string '2' in the array ? #Confused.

— Coerced ?

— Array index is integer

— U prolly mix up Array.prototype.includes and the object key in operator

— In just controls if the key exists

In your example you got the VALUES 1 and 2, but they got keys which are 0 and 1

Message permanent page

— Thnx Mate!🤝