1

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.

Sven
  • 12,997
  • 27
  • 90
  • 148
  • `in` checks if the **key** `num` exists in the `testArr` and not if there is **value** `num` in it. And as there is an element at the index `0` the result is true. You are looking for `testArr.indexOf(num)` [MDN: Array.prototype.indexOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) – t.niese Mar 13 '15 at 21:19

4 Answers4

2

In this case you returned that the index exists in the array but not the value. See this documentation in operator. I think that you must validate values of array with indexOf function.

andalm
  • 81
  • 6
1

You want to use the [].indexOf(elem) (doc) and check for -1 if it's not in the array. There's also a few other ways mentioned in this question How do I check if an array includes an object in JavaScript?

Your code would be updated as follows:

var testArr = [1, 2, 3, 4, 5];

function check(num) {
    if (num !== undefined) {
        if (testArr.indexOf(num) != -1) {
            alert("in");
        } else {
            alert("not in");
        }
    }
}

The in operator searches for keys and 0 will be a valid index in any nonempty array. in does not return true for all arrays. Try it with an empty array []

Community
  • 1
  • 1
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
1

"in" is checking by key. You have testArr[0] so you can get alert("in").

That is, if u use check(0)~check(4) will be alert("in"), other will be alert("not in") such as check(5).

var testArr = [1, 2, 3, 4, 5];

function check(num) {
  if (num in testArr) {
    return "check(" + i + ") = in ";
  } else {
    return "check(" + i + ") = not in ";
  }
}

for (var i = 0; i <= 6; i++) {
  document.write(check(i) + "<br>")
}
Sing
  • 3,942
  • 3
  • 29
  • 40
0

The prop parameter from the in operator will search a property or check if an index exists.

From the documentation:

prop: A string or numeric expression representing a property name or array index.

And from one of it's examples:

You must specify the index number, not the value at that index.

If you do:

<script>

var trees = new Array("redwood", "bay", "cedar", "oak", "maple");

'length' in trees // returns true because length is a property.

</script>

So in your case you are asking the check function if index 0 exists.

Victor Tello
  • 161
  • 13