> For the complete documentation index, see [llms.txt](https://hannahpun.gitbook.io/leetcode-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hannahpun.gitbook.io/leetcode-note/two-pointer/704.-binary-search.md).

# #704. Binary Search

````
```javascript
var search = function(nums, target) {
   let L = 0;
   let R = nums.length - 1;
   let mid;
   while(R >= L){
        mid = Math.floor((R+L)/2);
        if(target > nums[mid]){
            L = mid + 1;
        } else if(target < nums[mid]){
            R = mid - 1;
        } else {
            return mid;
        }
   }
   return -1;
};

```
````
