4

I've got small problem in understanding "in" operator in JavaScript. Why can't I go to the if block in example below? How do I do it?

var ar = [];
var a = 4;
ar.push(a);

if (a in ar){
    console.log("in if");
}

console.log(ar);
console.log(a);
console.log(typeof(ar[0]));
console.log(typeof(a));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    The `in` operator tests the **keys** not **values**. – Barmar Nov 03 '15 at 17:13
  • @dave An array is an object, it just has numeric keys. – Barmar Nov 03 '15 at 17:13
  • @dave in javascript arrays **are** objects, it's not working because there's no fifth item (fourth index) in the array – jonny Nov 03 '15 at 17:14
  • Does this answer your question? [Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?](https://stackoverflow.com/questions/3067072/why-does-javascripts-in-operator-return-true-when-testing-if-0-exists-in-an-a) – VLAZ Apr 02 '20 at 12:04

4 Answers4

5

The in operator tests whether the given property name exists in an object, it doesn't search the values. a in ar will be true if ar[a] exists. In your example, ar[0] exists, but ar[4] doesn't, so a in ar is false.

To search values in an array, use the indexOf function. It returns the array index of the found element, or -1 if the value can't be found.

if (ar.indexOf(a) != -1) {
    console.log("in if");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Thank you. I programmed a lot in python so i assumed that its the same as python. – nombreellos Nov 03 '15 at 17:37
  • Nope. It's also not the same when you write `for (x in obj)`, that sets `x` to the property names of the object, not the values. – Barmar Nov 03 '15 at 17:40
0

That's because the in operator is looking for property names, not property values.

It works fine for arrays, but you need to specify the index of the item, not the value:

var ar = [];
var index = 4;
var value = 5;
ar[index] = value;

if (index in ar){
    console.log("in if");
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

If you are using in with Array you should use index/key and not the value. Below will have the if condition true.

Ex:

var ar = [];
var a = 4;
ar.push(a);
if(0 in ar){
  console.log("in if");
}

You can use property name when working with object. Ex:

var student = {name: "John", lastName: "Cena", age: 30};
if(name in student){
  console.log("in if");
}
Beginner
  • 670
  • 12
  • 25
0

Imagine an object and an array that looks like this:

var obj = { foo: "bar" };
var arr = ["bar"];

You would access the obj's bar value by invoking the corresponding key on that object, i.e.

obj.foo // "bar"

An alternative way of doing this would be to use the array syntax:

obj["foo"] // "bar"

As you can see, each of the object's keys has a name (i.e. foo). An array is still an object, only that it's 'keys' (or indexes) are incremental numeric values. Thusly, we access the various elements in the array using their respective indexes in that array, i.e:

arr[0] // "bar"

In this way, use of the in keyword should become clearer. MDN defines the in keyword as the following:

The in operator returns true if the specified property is in the specified object.

So, in keeping with our previous example, consider the following:

"foo" in obj // true
"bar" in obj // false

The above happens because, as you already know, the variable obj only has one element in it, and that element's key is foo, and it's value is bar. With this in mind, consider your array:

var ar = [];
var a = 4;
ar.push(a);

0 in ar // true
4 in ar // false

As you can now hopefully see, in much the same way it investigates the js object, for arrays the in keyword is still looking for the key and not for the value corresponding to that key. Though the value at index 0 is 4, it's key (or index) is still 0. Hence, it should be clear why calling

4 in ar // false

Returns false - there's nothing in that array's fourth index. Hope this helps.

jonny
  • 3,022
  • 1
  • 17
  • 30