JavaScript Program for Binary Search with Example and Explanation
while loop to repeatedly check the middle element of a sorted array and narrow the search range using low, high, and mid indexes until the target is found or the range is empty; example: function binarySearch(arr, target) { let low = 0, high = arr.length - 1; while (low <= high) { let mid = Math.floor((low + high) / 2); if (arr[mid] === target) return mid; else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return -1; }.Examples
How to Think About It
Algorithm
Code
function binarySearch(arr, target) { let low = 0; let high = arr.length - 1; while (low <= high) { let mid = Math.floor((low + high) / 2); if (arr[mid] === target) { return mid; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return -1; } console.log(binarySearch([1, 3, 5, 7, 9], 7)); // 3 console.log(binarySearch([2, 4, 6, 8, 10], 5)); // -1 console.log(binarySearch([10], 10)); // 0
Dry Run
Let's trace searching for 7 in [1, 3, 5, 7, 9] through the code
Initialize pointers
low = 0, high = 4 (array length - 1)
Calculate mid
mid = Math.floor((0 + 4) / 2) = 2, arr[mid] = 5
Compare mid value with target
5 < 7, so set low = mid + 1 = 3
Calculate new mid
mid = Math.floor((3 + 4) / 2) = 3, arr[mid] = 7
Check if found
arr[mid] === target, return 3
| low | high | mid | arr[mid] | action |
|---|---|---|---|---|
| 0 | 4 | 2 | 5 | 5 < 7, set low = 3 |
| 3 | 4 | 3 | 7 | 7 == 7, return 3 |
Why This Works
Step 1: Divide and conquer
The code splits the search range in half each time by calculating the middle index with mid = Math.floor((low + high) / 2).
Step 2: Compare middle element
It compares the middle element to the target using arr[mid] === target to decide if it found the target or needs to search left or right.
Step 3: Adjust search range
If the middle element is less than the target, it moves low up to mid + 1 to search the right half; otherwise, it moves high down to mid - 1 to search the left half.
Step 4: Return result
If the target is found, it returns the index; if the search range is empty (low > high), it returns -1 to indicate the target is not in the array.
Alternative Approaches
function binarySearchRecursive(arr, target, low = 0, high = arr.length - 1) { if (low > high) return -1; let mid = Math.floor((low + high) / 2); if (arr[mid] === target) return mid; else if (arr[mid] < target) return binarySearchRecursive(arr, target, mid + 1, high); else return binarySearchRecursive(arr, target, low, mid - 1); } console.log(binarySearchRecursive([1,3,5,7,9],7)); // 3
function linearSearch(arr, target) { return arr.indexOf(target); } console.log(linearSearch([1,3,5,7,9],7)); // 3
Complexity: O(log n) time, O(1) space
Time Complexity
Binary search halves the search space each step, so it runs in logarithmic time, O(log n), where n is the number of elements.
Space Complexity
The iterative version uses constant extra space O(1) because it only stores a few variables for indexes.
Which Approach is Fastest?
Iterative binary search is fastest and most memory-efficient; recursive uses more space due to call stack; linear search is slowest.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative Binary Search | O(log n) | O(1) | Large sorted arrays, memory efficient |
| Recursive Binary Search | O(log n) | O(log n) | Readable code, small arrays |
| Linear Search | O(n) | O(1) | Unsorted or very small arrays |
low or high pointers correctly, causing infinite loops or wrong results.