Take the following piece of code:
var testArr = [1, 2, 3, 4, 5];
function check(num) {
if (num !== undefined) {
if (num in testArr) {
alert("in");
} else {
alert("not in");
}
}
}
With the check()
function I try to check whether a given parameter is part of the testArr
array or not.
In general the function works fine, only when checking for 0
the condition is met, although 0
is not part of the array. See for yourself: Fiddle
Why is this happening?
One possible solution I could think of is that 0
is evaluated as false
in some cases (==
vs ===
), but when passing false
as argument, the condition is not met, which does not make any sense to me – I am puzzled.