Binary Search in Javascript

Search for a command to run...

No comments yet. Be the first to comment.
In this programming tutorial, you'll see a javascript function to count the frequency of words. Instead of using a third-party npm package, I had to create a custom script to return the number of words as JS Object for a CLI project. Code Here is the...
We’ve tried out lots of apps over time, and honestly—not all of them do what we really need. That’s why I’m writing this blog post: to share some of the challenges we’ve noticed with the Shopify Forms app and hear what others think too. If you’ve fou...
Hey everyone, Dorothi Viki here. For the last decade, I’ve been in the digital marketing trenches. I’ve managed budgets big and small, and I’ve seen firsthand how crucial it is to know what people are saying about you online. When you’re starting out...
If you’re a small business owner, freelancer, or consultant, your website is your storefront. But traditional website development can be expensive, slow, and intimidating. Platforms like SpreadSimple remove these barriers by allowing you to turn Goog...

For SEO professionals, content marketers, freelancers, and consultants, keyword research is non-negotiable. It’s the foundation of any effective search strategy. However, with so many tools behind paywalls, finding a reliable, high-quality, free Goog...
With the rise of AI agents and Claude-specific development environments, understanding Claude Code has become essential for modern developers, freelancers, and consultants. Whether you're building Multi-Component Programs (MCPs) or designing autonomo...
In this article, I am going to show you the importance of Binary Search and how to implement using Javascript. There are many articles out there talking about this algorithm, so I am not going to repeat it here.
It is also known as logarithmic search because of the time complexity O(log n) .
If you don't what time complexity means, you can check out this article to know more. In short, the time complexity is a way to find the runtime of an algorithm.
In a practical scenario, suppose you have an app with users with names from A to Z, then you are going to use a binary search algorithm starting from the middle to find the user name. By using this algorithm, you could find an element with 7 guesses from 100 items length array and that's the power of binary search.
For writing Javascript snippets, I use RunJS which is really easy to use. If you are doing coding practices, then I would suggest writing on a paper first and then Text Editor (NOT IDE).
1) In-built function
let arr = [2, 3, 5, 7, 10, 20 ,22, 25, 30, 33, 34, 37, 40, 46];
let searchVal = 10;
//To check if element is present or not
console.log(arr.includes(10)) // true
//To print search value index
console.log(arr.indexOf(10)) // 4
Check this one as it talks about Binary Search vs indexOf.
2) Binary Search
function binarySearch(arr, number) {
let start = 0; // start index
let end = arr.length - 1; //array length
//LOOP until start index great than end index
while (start <= end) {
let mid = Math.floor((start + end) / 2); //middle index
if (arr[mid] == number) return true;
else if (number < arr[mid]) {
end = mid - 1;
} else if(number > arr[mid]) {
start = mid+1;
}
}
return false;
}
//Driver code
let arr = [1, 3, 4, 6, 12, 35, 40, 45, 57];
let number = 45;
binarySearch(arr, number);
As you can see from the code,
Useful animation

Find the square root of a large number in O(logn) time. For example, find square root of 121. Hint: Use binary search.
If you need to add anything or found anything to update please use the comment below.