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).