In JavaScript, the in
operator is primarily used to check if a property exists in an object or if an index exists in an array.
However, it is not suitable for use with primitive types such as numbers, strings, or booleans because they are not objects and do not have
properties.
If the right operand is of primitive type, the in
operator raises a TypeError
.
let x = "Foo";
"length" in x; // Noncompliant: TypeError
0 in x; // Noncompliant: TypeError
"foobar" in x; // Noncompliant: TypeError
You should use the object equivalents of numbers, strings, or booleans if you really want to check property existence with the in
operator.
let x = new String("Foo");
"length" in x; // true
0 in x; // true
"foobar" in x; // false