I think (if I am not wrong), that when using && operator, the second parm will not even execute when the first evaluated to false.

So It would be wise to put the parm that will be more likely to be false at first, right?

When looping, like this:

function inArray(fnd,arr){
    var i=0,ien=arr.length;
    for(;i<ien;i++){
        if(i in arr&&arr[i]===fnd){
            return i;
        }
    }
    return -1;
}

Which would be better to use?

...
if (i in arr && arr[i] === fnd) {
    return i;
}
...

or

...
if (arr[i] === fnd && i in arr) {
    return i;
}
...
link|improve this question
feedback

2 Answers

Why are you using (i in arr) anyway? You already know it returns true, since you're not iterating out of the bounds of the array. if (arr[i] === fnd) return i; should be enough

link|improve this answer
1  
@mithril333221: Not an issue. At worst, arr[i] === undefined, in which case arr[i] === fnd should return false anyway (unless fnd is undefined as well, of course, in which case you have bigger issues that lie outside of this code). – cHao Feb 20 at 6:00
feedback

The first one is the only of those two that makes any sense.

In the second one you are first accessing the array, then checking if you can access the array, so the check is pointless.

link|improve this answer
not pointless, I think it will prevent this stackoverflow.com/a/9347202/1148349 – mithril333221 Feb 19 at 22:09
2  
@mithril333221: What would checking the index after using it prevent? – Guffa Feb 19 at 22:23
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.