Why Does JavaScript's "In" Operator Return True When Testing If 0 Exists in an Array That Doesn't Contain 0

Why does javascript's in operator return true when testing if 0 exists in an array that doesn't contain 0?

It refers to the index or key, not the value. 0 and 1 are the valid indices for that array. There are also valid keys, including "length" and "toString". Try 2 in x. That will be false (since JavaScript arrays are 0-indexed).

See the MDN documentation.

Why (thisIsInTheArray in array) returns false?

According to documentation:

The in operator returns true if the specified property is in the
specified object or its prototype chain.

Use array.includes() instead.

PS: There are a lot of interesting stuff in MDN documentation ;)

Javascript for in

Can someone please explain how the JS engine is interpreting for in?

None of those examples uses for-in, which looks like this:

for (const key in something)

They just use the in operator, which is something else entirely (though, perhaps, somewhat related).

Two things come into play in that code:

  • The in operator checks to see if the property named on the left (the name is converted to string if needed) exists in the object on the right (on the object itself, or on any of its prototypes).
  • Arrays are objects. An array index is actually a property name.

Picking up on that second point, let's look at the array [1,2,3,4,5]:

+−−−−−−−−−−−−−−−+
| (array) |
+−−−−−−−−−−−−−−−+
| "length": 5 | Property name is "length", value is 5
| "0": 1 | Property name is "0", value is 1
| "1": 2 |
| "2": 3 |
| "3": 4 |
| "4": 5 |
| [[Prototype]] |−−−−>Array.prototype, which has various methods
+−−−−−−−−−−−−−−−+

Based on that information:

const array = [1,2,3,4,5];
console.log(0 in array);

in converts 0 to "0" and checks for a property with that name on array. There is one, so the result is true.

const array = [1,2,3,4,5];
console.log("0" in array);

in checks for a property called "0" property on array. There is one, so the result is true.

console.log(4 in [1,2,3,4,5]);

in converts 4 to "4" and checks for a property with that name on [1,2,3,4,5]. There is one, so the result is true.

console.log(4 in [1,2,3,4]);

in converts 4 to "4" and checks for a property with that name on [1,2,3,4]. There isn't one, so the result is false.


The conversions of numbers to strings mentioned above are per the specification, but JavaScript engines optimize arrays where possible, and if dealing with such an optimized array, it can avoid converting the number to string before doing the lookup.

JavaScript array.every() method returns false when using the in operator?

You need to check with Array#includes.

The in operator checks if a key of an object exists.

const    isSubSet = (universalSet, subSet) =>        subSet.every(element => universalSet.includes(element));
const [setA, setB, setC] = [[1, 2, 3, 4, 5], [1, 2, 3], [1, 2, 3, 4, 5]]const [checkA, checkB] = [, isSubSet(setA, setC)]
console.log(`is setB subset of setA: ${isSubSet(setA, setB)}`)console.log(`is setC subset of setA: ${isSubSet(setA, setC)}`)

why `1 in [1, 2]` is true but `1 in [1]` is false?

Array is also an object.

Array looks like this:

{ 
0: 1,
1: 2,
2: 3
length: 3
}

in operator looks for the property name in the object.

so when you do 1 in [1] it's false because object looks like this...

 {
0: 1,
length: 1
}

Notice there is no key named 1 it ends at 0, also known as index.

you can do this too 1 in ['a','b'], which is true...

So in short, Arrays in JS are objects and their indexes are keys in the object. The in operator matches with keys which happen to be index.

Check Element not in array in java script?

Becase the in operator checks whether a property is defined in an object.

The array indexes are the "properties" of the array objects, thus, the 2 property is the index 2, which contains the value 3.



Related Topics



Leave a reply



Submit