check if two elements exist in array with older browser support Currently I have this code

function foo(firstId, secondId){
    var boolOne = false;
    var boolTwo = false;

    for (var i = 0; i &lt; arr.length; i++) {
        var current = arr[i];
        var id = current.id;

        if(firstId == id){
            boolOne = true;
        }

        if(secondId == id){
            boolTwo = true;
        }

        if(boolOne &amp;&amp; boolTwo){
            break;
        }
    }

    if(boolOne &amp;&amp; boolTwo){
        bar();
    } else {
        // throw error
    }
}


I want to check if two objects with the given parameter ids already exist in the array.

I put my code into one for-loop but I could also split the code into two loops and break on a match.

I know I could also use 

var boolOne = arr.includes(item =&gt; item.id == firstId)

but I have to support the Internet Explorer 9+. Is there something that can be optimized?
From a short look


foo is a terrible name


If this is not the real name, then please do submit the real code

arr seems to be a global, bad practice
You don't need current, you could just go for var id = arr[i].id; 
For older browsers, it is faster to cache arr.length instead of retrieving it every time
bar is a terrible name, I really hope you don't write production code like that
the call to bar should probably happen outside of that function


Given that, I would go for the below pseudo code:

function arrayHasBothObjects(list, id1, id2){
    var boolOne = false,
        boolTwo = false,
        size = list.length;

    for (var i = 0; i &lt; size; i++) {
        var id = list[i].id;

        boolOne = boolOne || id == id1;
        boolTwo = boolTwo || id == id2;

        if(boolOne &amp;&amp; boolTwo){
            return true;
        }
    }
    return false;
}