-1
1 in [1, 2] //true
"foo" in ["foo", "bar"] //false
1 in [1] //false - ? what's going on here?
A. Duff
  • 4,097
  • 7
  • 38
  • 69
  • 3
    `in` is not `indexOf`, reading the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) might help. – Teemu Nov 16 '15 at 16:29
  • 2
    Please use the search before you ask a new question. – Felix Kling Nov 16 '15 at 16:36
  • I did, actually. And I found `indexOf`. But that still didn't explain why `in` wasn't working. umad that people here actually want to learn? – A. Duff Nov 16 '15 at 17:04

4 Answers4

4

The in operator does not check if the element is in an array (like Python or JS Array's indexOf), it checks if the object has a property with the given name.

You want to use indexOf here, since that actually checks to see if the value is contained in the array. That will work with numbers and strings, but not always objects (you must provide the exact same object, not equivalent ones).

In example #1, an array with two elements has an element named 1 (the second element in the array). In example #3, an array with one element does not.

Since array elements are numbered, there is no foo element in any array.

If you were to use:

var data = ["foo", "bar"];
data.foo = "baz";

then "foo" in data would return true.

ssube
  • 47,010
  • 7
  • 103
  • 140
2

that in foo tests to see if there is a property named that, not if there is a property with a value that matches that.

x = [1, 2] is the same as x = []; x[0] = 1; x[1] = 2. x[1] exists so 1 in xis true.

x = ["foo", "bar"] is the same as x = []; x[0] = "foo"; x[1] = "bar". x[1] doesn't exist so 1 in xis false.

You're looking for the indexOf method.

["foo", "bar"].indexOf("foo") >= 0
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

The in operator checks for the existence of a given key on your array, which means that:

1 in [1, 2] // Checks if arr[1] exists
"foo" in ["foo", "bar"] // Checks if arr['foo'] exists
1 in [1] // Checks again if arr[1] exists

Remember that array's keys are by default numeric indexes, so [1, 2] and ['foo', 'bar'] contains 0 and 1 keys and [1] contains only 0 key.

If you want to check whether an array contains a given value, you should propably use the Array#indexOf method:

[1, 2].indexOf(1) !== -1 // true
["foo", "bar"].indexOf("foo") !== -1 // true
[1].indexOf(1) // true

You might want to read the MDN docs about the in operator.

Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
2

ther is the "for-in" that is used to cycle associative arrays, like this

for( i in myArray ) {
  // Do something with myArray[ i ];
}

and "in" instruction ("x in y") is true if "y" has an element with KEY x and not VALUE x

If you want to test the existance of a data in an array

if( myArray.indexOf( "foo" ) > -1 ) {
  // "foo" is there
}
RiccardoC
  • 876
  • 5
  • 10
  • And as I said in the subsequent row, the simple "in" do not give you the presence of an element in an array, but the presence of a key in an array – RiccardoC Nov 16 '15 at 16:37
  • You're right, that's was not correct by my wording, I'll correct – RiccardoC Nov 16 '15 at 16:42