---
description: 典型 Binary Search，運用 L R
---

# #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;
};

```
````
