JavaScript Algorithms: Binary Search

Simple implementation of the binary search algorithm in JavaScript; Underscore's _.sortedIndex()

HTML

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<p class="result1"></p>

JavaScript

//var arr = [5, 8, 53, 56, 123, 322, 400, 2356, 8000, 23333];
var arr = [5, 8];


function binarySearch(array, key) {
    var middle = Math.round(array.length / 2),
        left = 0,
        right = array.length;
    while (right >= left) {
        if (array[middle] === key) {
            return middle;
        } else if (array[middle] > key) {
            right = middle - 1;
        } else {
            left = middle + 1;
        }
        middle = Math.floor((left + right) / 2);
    }
    return -1;
}

var bSearch = binarySearch(arr, 8)
$('.result1').html("generic binarySearch: " + bSearch)