let arr = [1,2,3,4,5,6,7,8,9];
let n = 9;
let key = 6;

//Search Element Using BinarySearch by divide the array into internal's
function BinarySearch(arr,key){
    var low = 0;
    var high = arr.length-1;
    
    while(high >= low){
        var m = low +  Math.floor((high-low)/2);
        
        if(arr[m] == key)
            return m;
        else if(arr[m] < key)
            low = m+1;
        else if(arr[m]>key)
            high = m-1;
    }
    return -1;
}

var result = BinarySearch(arr,key);
console.log(key,'at index numer',result);

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: