1

Why this statement returns false? It's really weird

console.log("100038916831294" in ["100003748210938", "100038916831294"]);
Vemu
  • 322
  • 1
  • 5
  • 13

2 Answers2

8

The in operator tells you whether a value exists as a property name in an object. The property names of your array are "0" and "1".

You can use one of the Array methods to check if a value is in the array, like .indexOf() or .includes():

console.log(["100003748210938", "100038916831294"].includes("100038916831294"));
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

The in operator in JavaScript compares indexes or property names in arrays instead of the value itself.

For example, if we write console.log(0 in ["abc","pqr"]); it will print true. However, if we use the value, like in console.log("abc" in ["abc","pqr"]); it will print false.

You can further read about it on https://www.w3schools.com/jsref/jsref_operators.asp.

LostMyGlasses
  • 3,074
  • 20
  • 28