I have seen many times things like this:
function customArrayIndexOf(item, array){
for (var i = 0, l = array.length; i < l; i++) {
if (i in array && array[i] === item) return i;
}
return -1;
}
However, I am not sure why is the i in array needed.
I have three questions:
- What it is doing?
- Is it necessary?
- When it will fail without it?
Sparse arrays may cause a false positive without the check:
(
iandlshould be local, i.e. declared withvar)