Binary Search is one of those classic algorithms that every developer should understand — not just to ace interviews, but to truly level up how you think about performance and problem-solving. Whether you’re building a real-time search UI, navigating sorted data, or optimizing lookups, this algorithm shines in its simplicity and speed.
In this post, we’ll break down what binary search is, how to implement it in JavaScript (iteratively and recursively), and explore real-world applications + common variations you might face in interviews.
Binary search is an efficient algorithm for finding a target value in a sorted array.
Instead of scanning each element one by one (like linear search), binary search:
This “divide and conquer” strategy makes it O(log n) time complexity — ideal for large datasets.
function binarySearch(arr, target) {let left = 0;let right = arr.length - 1;while (left <= right) {const mid = Math.floor((left + right) / 2);if (arr[mid] === target) return mid;else if (arr[mid] < target) left = mid + 1;else right = mid - 1;}return -1; // Target not found}
binarySearch([1, 3, 5, 7, 9, 11], 7); // ➞ 3 (index)
function binarySearchRecursive(arr, target, left = 0, right = arr.length - 1) {if (left > right) return -1;const mid = Math.floor((left + right) / 2);if (arr[mid] === target) return mid;if (arr[mid] < target)return binarySearchRecursive(arr, target, mid + 1, right);elsereturn binarySearchRecursive(arr, target, left, mid - 1);}
✅ Great for large datasets
✅ Reduces time complexity from O(n) to O(log n)
✅ Perfect fit for sorted arrays or data structures like binary search trees
Binary search is the base of many advanced problems:
Search Insert Position
Return index where target should go if not found.
Find First or Last Occurrence
When array has duplicates.
Binary Search in Rotated Sorted Array
(LeetCode classic) — slightly trickier.
Infinite Sorted Array
Simulate by expanding window size exponentially.
2D Matrix Binary Search
Flatten the 2D array mentally into a 1D view.
Binary search is more than just a whiteboard algorithm — it’s a mindset. Once you truly understand how it cuts your problem space in half each step, you’ll start spotting optimization opportunities everywhere in your code.
So the next time you’re looping through a sorted list or working on search features — stop and ask yourself: “Can I binary search this?”
Try writing a version of binary search that returns the index of the closest number if the target isn’t found. That’s a common real-world twist.
Quick Links
Legal Stuff
Social Media