# Binary search algorithm in javaScript

### Sai gowtham Nov 16

In computer science, the binary search algorithm is used to find the position of the target value in a sorted array.

### How binary search works?

Binary search algorithm compares the target value to the middle element in the array. if the target is found then return the index or search continues in the other half of the array repeating until a target is found.

Let's learn by using a sample sorted array.

### Binary search algorithm implementation.

- store the midpoint of the array.
- compare the midpoint to the target value.
- return item

- if midpoint is less than the target value then search continues in the upper half of the array.
- if midpoint is greater than the target value then search continues in the lower half of the array.

```
function binarySearch(arr, target) {
const midpoint = Math.floor(arr.length / 2);
if (arr[midpoint] === target) return arr[midpoint];
if (arr[midpoint] < target && arr.length > 1) {
return binarySearch(arr.slice(midpoint), target);
}
if (arr[midpoint] > target && arr.length > 1) {
return binarySearch(arr.slice(0, midpoint), target);
}
return false;
}
console.log(binarySearch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 10)); //10
```

### Tests

Hope you enjoyed...

dev.to is where software developers read, write, and level up.
Sign Up Now

(open source and free forever β€οΈ)

(open source and free forever β€οΈ)

Recursive functions are unpredictable, consume more memory and can crash your browser with a large dataset. Binary search iterative version is so simple that I never saw a recursive implementation.

Returning "notfound" is so wrong, what if you have an array of strings?

You can return the Index of the value or -1 if not found, or a null value.

I have to agree with you on this one. it can be simpler. returning bool won't do.

The time complexity of binary search is O(logn)

It's doesn't matter how much longer the data set is it always takes one extra step to find the target.

Suppose if we have 8 elements.

log

_{2}8 = 3 π 2^{3}= 8 . (2x2x2 = 8)It only takes 3 steps to find the target.

For 16 elements.

log

_{2}16 = 4It only takes 4 steps to find the target.

For 32 elements

log

_{2}32 = 5It only takes 5 steps to find the target in 32 elements data set.

I know but that is not an excuse to write more complex code that is less efficient and consumes more memory, in my opinion of course. 3 extra stacks and 5 variables will not affect the 2GB Chrome memory hog of course, but we have to start writing better code eventually.

Also providing some Defensive programming in these kind of articles would help raise awareness among the web developers, for example at least check if the collection is an array or a string and throw exceptions if something is malformed or missing.

I know that is only an example but junior developers only see this kind of code, and will have the false assumption that the production code should look like this too.

What, exactly, is

bettercode? Is it optimized for readability and understandability, or for performance?In my opinion, writing your code in a clear and concise way should be step one, and only change the code for more performance if it is necessary. With the binary search function in Javascript, there are so many things going on that this would be nowhere near the top of things to optimize (except maybe in very exceptional cases).

If things work slow, it's a good skill to know how to optimize for performance β but it is at least as important to know which parts take the most time and prioritizing what to work on first. And my guess is that in most cases, the binary search isn't what will take the most time.

In this case is none of them, like I said in my first comment, it is a non-standard implementation (recursive, while most of the examples I could find are iterative), and less performant.