javascript - What is the difference between `var in array` and `array.indexOf(var)`? - Stack Overflow

admin2025-04-19  0

I am trying to get my head around arrays in JS. My question is; are the following two tests equivalent?

var test = 2;
console.log(test in [1,2,3]);
console.log([1,2,3].indexOf(test) != -1);

They seem to be, but then answers like this and the book I am reading suggest that you can't do in on an array, only on an object. Looking for clarity. There must be a reason that people use .indexOf(x) (which I assume is linear time) and not in (which I assume is constant time).

I am trying to get my head around arrays in JS. My question is; are the following two tests equivalent?

var test = 2;
console.log(test in [1,2,3]);
console.log([1,2,3].indexOf(test) != -1);

They seem to be, but then answers like this and the book I am reading suggest that you can't do in on an array, only on an object. Looking for clarity. There must be a reason that people use .indexOf(x) (which I assume is linear time) and not in (which I assume is constant time).

Share edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Nov 30, 2016 at 11:12 AidenhjjAidenhjj 1,2891 gold badge14 silver badges28 bronze badges 1
  • 1 using in is looking at the object keys, for an array they are number keys rather than strings. indexOf is looking at the values of the array for the item and returning the index. – synthet1c Commented Nov 30, 2016 at 11:15
Add a ment  | 

5 Answers 5

Reset to default 7

No. They are pletely different.

test in [1,2,3] checks if there is a property named 2 in the object. There is, it has the value 3.

[1,2,3].indexOf(test) gets the first property with the value 2 (which is in the property named 1)

suggest that you can't do in on an array, only on an object

Arrays are objects. (A subclass if we want to use classical OO terminally, which doesn't really fit for a prototypal language like JS, but it gets the point across).

The array [1, 2, 3] is like an object { "0": 1, "1": 2, "2": 3 } (but inherits a bunch of other properties from the Array constructor).

As per MDN,

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

in will check for keys. Its similar to Object.keys(array).indexOf(test)

var a1 = [1,2,3];
var a2 = ['a', 'b', 'c']
var test = 2;

console.log(test in a1)
console.log(test in a2)

// Ideal way

//ES5
console.log(a1.indexOf(test)>-1)
console.log(a2.indexOf(test)>-1)

//ES6
console.log(a1.includes(test))
console.log(a2.includes(test))

The first checks for an index, or if property of an object exists,

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

and not for a value in the array, as the second is doing.

console.log([1,2,3].indexOf(test) != -1);

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

Using in operator for an array checks of the indices and the length property - because those are the properties for an array:

console.log(Object.getOwnPropertyNames([1, 2, 3]));
console.log(Object.keys([1, 2, 3]));
console.log('length' in [1, 2, 3]);

Object.keys : return all enumerable properties

Object.getOwnPropertyNames : return all properties

Try this

var test = 2;
var arrval= [1, 5, 2, 4];
var a = arrval.indexOf(test);
if(a>-1) console.log("Having");
else console.log("Not Having");
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745058125a282528.html

最新回复(0)