var inArray = function(a, b, c, d) { for (c in b) d |= b[c] === a; return !!d };
This is terrible code, and you should run away from it. Bitwise operators are completely unnecessary here, and the parameters c and d do not make any sense (as Raymond Chen noted, the author of the code most likely did this for the safe space for declaring local variables - a problem, although it is that if true is passed for d , the code abruptly interrupted, and additional parameters destroy any understanding that would provide an idea of ββthe declaration).
I will explain the code, but first here is the best option:
function inArray(arr, obj) { for (var i = 0; i < arr.length; ++i) { if (arr[i] === obj) { return true; } } return false; }
Note that this depends on whether the array is the actual array. You can use a loop like for (k in arr) to generalize it to all objects.
In any case, explain:
for (c in b) d |= b[c] === a;
This means that for each key in b (stored in c ) we will check if b[c] === a . In other words, we do a linear scan over the array and check each element on a .
d |= val is bitwise or. Bits with a high val value will be set to d . This is easier to illustrate in languages ββwhere bits are more affected than in JS, but their simple illustration is:
10011011 01000001 -------- 11011011
This is simply OR'ing each individual bit with the same location bit in a different value.
The reason this abuse is because it overthrows the code and depends on strange implicit garbage.
x === y returns a boolean value. The logical value used in bitwise terms makes little sense. However, what happens is that the logical value is converted to a non-zero value (possibly 1 ).
Similarly, undefined will be d . This means that d will be passed to 0 for bitwise material.
0 | 0 = 0 0 | 0 = 0 , but 0 | 1 = 1 0 | 1 = 1 . So basically it is glorified:
for (c in b) d = (d || (b[c] === a));
As for !!x , which is just used to put something into a bool. !x will take x, implicitly drop it into bool and then cancel. Optional ! then denies it again. So it most likely implicitly results in bool ( !!x being true means that x is at least weakly true ( 1 , "string" , etc.), A !!x implies that x at least weakly false ( 0 , "" , etc.).
This answer offers some more options. Please note that they are all designed to indexOf native indexOf , which will almost certainly be faster than anything we can encode in script -land.