Binary Search in JavaScript18 Apr 2025 | 6 min read What is Binary search in JavaScript?In JavaScript, binary search is a technique that is used for searching and works on the divide-and-conquer approach. With the help of binary search, we can search for any element in a sorted array. In JavaScript, binary search divides the array into half until the element is found. With each repetition, it eliminates half of the remaining elements, which results in a significantly faster search time compared to linear search. When we compare binary search with the linear search, it is much faster, and the complexity of binary search is O(logN), whereas linear search works in O(N) time complexity. In simple words, it is a searching technique that uses the divide and conquer approach that divide the problem into simpler problems until it becomes simple enough to solve them directly. Binary search is a simple, user-friendly, and efficient search algorithm. Implementation of Binary search in JavaScriptLet's take an example: Find out the index of "26" out of a sorted array that is given below. [ 2, 6, 10, 14, 18, 22, 26, 30, 34] Let's see the step-by-step process to apply binary search in the above example:
Types of approaches in Binary searchThere are two ways/approach to do binary search in JavaScript:
Recursive approach
Example Output: Value found! Value not found! Iterative approachIn this iterative approach, instead of recursion, we use the while loop, and the loop runs until it hits the base condition, i.e. start becomes greater than the end. Example Let's take an example to understand the implementation of binary search using an iterative approach. Output: Value not found! Value found! Time and space complexity of Binary searchBinary search in JavaScript has a time complexity of O(logN); here, N is nothing but the number of elements or values that are present there in an array list. But when it comes to the comparison with the linear search, which has a time complexity of O(N), that is why binary search is far better than linear search. In JavaScript, binary search performs all the operations over that same original array; it doesn't create the new array, so we can say that binary search works in O(1) space. In both of the cases, the recursive and iterative one, the Time complexity will be O(logN), and the Auxiliary space will be O(1). Application of Binary search
Advantages of Binary searchEfficient In JavaScript, binary search has a time complexity of O(log n), which makes it very efficient for searching large sorted arrays. Simple Implementation Binary search in JavaScript is relatively easy to implement and understand. Versatile In JavaScript, binary search is very versatile, and it can be used in a wide variety of applications. Reliable Binary search can be a reliable algorithm that will always find the target element if it exists in the array. Disadvantages of Binary searchRequires a sorted array In JavaScript, binary search only works on sorted arrays. If the array is not sorted, it must be sorted before binary search can be used. Not suitable for unsorted data Binary search is not suitable for searching in unsorted data as it will not be able to find the target element efficiently. May not be the best choice for large arrays For very large arrays, other search algorithms, such as interpolation search or hash tables, may be more efficient. Next TopicImport Export in JavaScript |
We request you to subscribe our newsletter for upcoming updates.