TL;DR
Binary search finds an item in a sorted collection by checking the middle element. If it matches, we're done. If our target is bigger, we ignore the left half and repeat on the right half, and vice versa. We keep dividing until we find the value or know it isn't there, which beats scanning every item.
- The collection must already be sorted before binary search can work. That's a prerequisite, not an optional step.
- With an even number of items, there are two middle candidates. This implementation picks the one on the left, which is how we land on 32 in the example.
- The iterative version is the efficient one because it doesn't inflate the function call stack. The recursive version adds a call for every level of the search.
- Calling binarySearch(numbers, 60) on the sorted numbers array returns 6, the index position where 60 lives.
Earlier, we looked at linear search, a simple algorithm that works by scanning every item in a collection until it finds what it is looking for. While simple is good, this approach is slow. The more items you have, the longer it will take to find something. Its running time is O(n). There is a faster way to search, and that is the star of this article: the binary search.
In the following sections, we will learn all about binary search, how it is implemented, its performance characteristics, and a whole lot more.
Onwards!
Binary Search in Action
A binary search starts off the same way as many great search algorithms do. It starts with a collection of items:

In this collection, what we want to do is find the number 60 and return its index position. The rest of our steps will walk through how we can use binary search to accomplish this.
Does Binary Search Need Sorted Items?
Before we even take our first step, there is an important prerequisite that we need to meet. Our collection of items must already be sorted, so let’s go ahead and do that:

In the future, we’ll look into the various ways we have to properly take an unsorted collection and sort the items inside it. For now, let’s just keep it simple and ensure that the items we throw into our binary search algorithm are already sorted.
How Do We Pick the Middle Element?
With the paperwork out of the way, the first real thing we do is find the middle element of our entire collection and check if the value here is the target we are looking for. For our collection, the middle element is 32:

How did we get 32? This is especially confusing when we have an even number of items as we do in this example. Let’s talk about this for a brief moment.
When dealing with a collection of odd numbers of elements, we have a clear middle element we can pick:

As we see here, we have five elements. The third element would be the middle.
For arrays with even numbers of elements, there is a trick we need to use. The middle element will be one that is to the left of the ideal midpoint. This can be visualized as follows:

With six elements there are two middle candidates. This implementation chooses the one on the left; choosing the one on the right can also work if our bound updates preserve the remaining candidates and always make progress. Applying our lower-middle convention to the larger example explains why we chose 32:

Later on, we'll look at a more formal way of finding the middle element that takes our hand-wavy explanation into something more concrete. For now, we are good!
OK, where were we? Yes! We need to check if our middle element contains the target value of 60 that we are looking for. We can tell that 32 is not equal to 60. What we do next is the famed division operation that binary search is known for.
Dividing FTW!
With our middle element not matching our target value, our next step is to figure out where to go and continue our search. We put our finger on the middle element and mentally divide our list into a left half and a right half:

For this next step, we ask ourselves whether the value we are looking for (60) is greater than or less than the value in our current middle element (32):
- If the value we are looking for is greater than our middle element value, the value we are looking for must be in the right half
- If the value we are looking for is less than our middle element value, the value we are looking for must be in the left half
In our case, 60 is greater than 32, so it must mean that we need to continue our search by looking in the right half:

At this moment, we only care about what is contained in the right half of our collection. We can safely ignore all other items. To be really specific, this means our earlier midpoint and all items left of it are no longer relevant for our future search activities:

With only the right half of our collection in play, we repeat our earlier steps. We find the middle element, check if the middle element’s value matches the target value we are looking for, and if the value doesn’t match, divide and decide whether to go deeper into the remaining left half or right half of our collection.
If we apply these steps to the right half, the first thing we do is find our middle element:

The middle element value here is 71, and it isn’t the 60 value we are looking for. Next, we check if 71 is greater than or less than our target 60 value. Because 71 is greater than 60, it means the half of the collection we want to focus on is the left half:

When we look at our left half, we only have two items. It doesn’t matter how many items we have, we must still follow our binary search algorithm step-by-step. We must rinse and repeat what we’ve done a few times already. The value of our middle element will be 40:

Our 40 value is not the same as 60. Because our current middle point value of 40 is less than 60, we focus on the right half:

We only have one item at this point, and this item will also be our middle element for the next step:

The value of our middle element is going to be 60. When we ask ourselves if the middle element value of 60 is what we are looking for, the answer will be a ginormous YES. This is exactly what we have been looking for, and we will return the index position of where our 60 value is located.
The JavaScript Implementation
If we take all of our many words and visuals from the previous section and simplify how binary search works, we can land on these steps:
- Look at the middle value in a sorted collection.
- If our middle value is the target value we are looking for, stop searching and return its index position
- If it is greater than the value we are looking for, ignore the right half of the collection and repeat step 1 on the left half
- If it is less than the value we are looking for, ignore the left half of the collection and repeat step 1 on the right half
- Keep repeating steps 1 to 4 until we find the number we are looking for or determine the value we are looking for is not in the collection
All that remains is to turn these steps into code.
Iterative Approach
The efficient iterative approach looks as follows:
// Iterative Approach
function binarySearch(arr, val) {
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middleIndex = Math.floor((start + end) / 2);
if (arr[middleIndex] === val) {
return middleIndex;
} else if (arr[middleIndex] < val) {
start = middleIndex + 1;
} else {
end = middleIndex - 1;
}
}
return -1;
}
For reasons we saw in the Fibonacci and Going Beyond Recursion article, this approach doesn't inflate the function call stack. We keep a lot of the heavy lifting localized to the loop itself.
Recursive Approach
For completeness, a less efficient recursive approach is provided below as well:
// Recursive Approach
function binarySearch(arr, val, start = 0, end = arr.length - 1) {
const middleIndex = Math.floor((start + end) / 2);
if (val === arr[middleIndex]) {
return middleIndex;
}
if (start >= end) {
return -1;
}
if (val < arr[middleIndex]) {
return binarySearch(arr, val, start, middleIndex - 1);
} else {
return binarySearch(arr, val, middleIndex + 1, end);
}
}
With this approach, when we are about to do a division, we recursively call binarySearch again. The number of times we will call binarySearch and inflate our call stack is directly related to how deep we will have to search. This is where the efficiency of the iterative approach really shines.
Example of the Code at Work
The way we would use our binarySearch function is by calling it with two arguments:
- Our presorted array of items
- The target value we are looking for
Below is an example of how we can call binarySearch:
let numbers = [1, 3, 5, 10, 32, 40, 60, 71, 80, 99];
let foundIndex = binarySearch(numbers, 60);
console.log(foundIndex) // 6
In our example, the numbers array is the same collection we spent much of this article deconstructing, so it should look welcomingly familiar. The value that gets returned by our binarySearch function is the index position of the found item or -1 if the item isn't found. In our case, 60 exists and is located at index position 6. Also, outside of the runtime behavior we briefly talked about, the values that get returned are the same across both the iterative and recursive binary search implementations.
Finding the Middle Element
Much earlier, we looked at a very hand-wavy approach to finding the middle element. For the full enchilada, this article goes into great detail on this topic. What we are going to do is briefly summarize that earlier article and shine some light on how our code works for finding the middle element, especially as part of a subset of our array.
The expression for calculating the index position of our middle element will look as follows:
let middleIndex = Math.floor((left + right) / 2);
We take the average of the starting and ending index positions, and then round the result down (via Math.floor) to ensure we always end on a whole number. If we had to visualize this, here is how we would calculate the middle point for the highlighted region taken from an intermediate step from the walkthrough e we were looking at earlier:

The values for left and right are the corresponding 5 and 9 index positions of our array, so if we substitute in those values and calculate the middle point, the earlier expression will look as follows:

Our middle index position for this region is 7, and the value here is 71. A trippy detail to note is that, even though we are only examining a subset of our collection, our index positions are relative to the entire collection itself.
How Fast Is Binary Search?
We started off by talking about how fast binary search is, especially when we compare it to the slower linear search. Binary search runs at a scorching O(log n), and it is able to pull this off because it follows a divide and discard approach. As we saw in the walkthrough, at each stage of trying to hone in on our target value, our binary search algorithm ignores half of the remaining items.
If we had to talk about this generally, let's say we start with a sorted collection of n items:

In the first step, we will be working with the full n items. At the next step, we will be working with n/2 items:

Assuming we never find the value we are looking for (or the value we are looking for is the very last item), this pattern will keep going where each step discards another half of the items from the previous step:

We will keep going until we reach the last step, where all we are left with is just a single element. There is a pattern here. We can see this pattern by observing the number of elements in play at each step:

After k divide steps, the approximate number of remaining candidates is n / 2k. This describes the size of the search space, not the number of steps already taken. Setting it to 1 explains the logarithmic growth:

The number of middle-element inspections grows as O(log n). Rounding and inspecting the final candidate matter for exact counts: for n > 0, the iterative code needs at most floor(log2(n)) + 1 inspections.
Frequently Asked Questions
What Is Binary Search?
Binary search is a fast way to find an item in a sorted collection. It checks the middle element, and if that isn’t the target, it ignores the half that can’t contain our value and repeats on the remaining half. It keeps dividing until it finds the value or knows it isn’t there, which beats scanning every item like linear search does.
How Does Binary Search Work?
Look at the middle value in a sorted collection. If it’s the target, return its index position. If the middle value is greater than our target, ignore the right half and repeat on the left half. If it’s less, ignore the left half and repeat on the right half, and keep going until we find the value or determine it isn’t in the collection.
Does Binary Search Need a Sorted Array?
Yes. Our collection of items must already be sorted before we even take our first step. That’s a prerequisite, not an optional step.
How Do You Find the Middle in Binary Search?
We take the average of the left and right index positions and round down with Math.floor: Math.floor((left + right) / 2). With an even number of items, there are two middle candidates, and this rounding picks the one on the left. Even when we’re only examining a subset of the collection, the index positions are relative to the entire collection.
How Fast Is Binary Search?
Binary search runs at a scorching O(log n), compared to linear search’s O(n). It pulls this off by ignoring half of the remaining items at each step. For n > 0, the iterative code needs at most floor(log2(n)) + 1 middle-element inspections.
Should Binary Search Be Iterative or Recursive?
The iterative approach is the efficient one because it doesn’t inflate the function call stack. The recursive version calls binarySearch again at each division, so the call stack grows with how deep the search goes. Both return the same values: the index position of the found item, or -1 if it isn’t found.
Conclusion
We just covered a lot of ground here. We learned how binary search works, how we can use it in our code, what its runtime behavior looks like, and a whole lot more. What makes binary search interesting is that its core approach of dividing the problem space into something smaller is a common pattern that we'll repeatedly see, especially in algorithms that classify themselves as divide and conquer. We will get to those algorithms shortly, but you should kick back and relax for now. You've earned it!
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Binary Tree Traversal, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

