Binary Search program in JavaScript

The binary search algorithm works on the divide and conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, the array should be sorted.

In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left sub-array, right sub-array, or return the middle element.

In this article, we are given a sorted array of integers, and our task is to search for the given target element using binary search algorithm.

How Binary Search Works

Binary search follows these key steps:

  1. Start with the entire sorted array
  2. Find the middle element
  3. Compare the target with the middle element
  4. If target equals middle element, return its index
  5. If target is smaller, search the left half
  6. If target is larger, search the right half
  7. Repeat until element is found or search space is exhausted
Binary Search Process 1 3 5 7 9 left mid right Compare with target Target = 7 5 < 7, search right half Next: compare 7 with 8 Found at index 3!

Example Scenarios

Scenario 1: Element Found

Input: arr[] = {1, 3, 5, 7, 9}, target = 7
Output: Element 7 found at index: 3

Scenario 2: Element Not Found

Input: arr[] = {2, 4, 6, 8, 10}, target = 5
Output: Element 5 not found

The approaches to implement binary search in Javascript are given below:

Binary Search Using Iteration

The iterative approach uses a loop to repeatedly narrow down the search space:

  • Initialize left and right pointers
  • Calculate the middle index in each iteration
  • Compare the target with the middle element
  • Adjust pointers based on comparison result
  • Continue until element is found or search space is exhausted

Example

function binSearchIterative(arr, target) {
    let left = 0;
    let right = arr.length - 1;
    
    while (left 

Given array: 1 3 5 7 9
Target element: 7
Element 7 found at index: 3

Binary Search Using Recursion

The recursive approach breaks down the problem into smaller subproblems:

  • Base case: return -1 if left index exceeds right index
  • Calculate middle index and compare with target
  • Recursively search left or right half based on comparison
  • Return the result from recursive calls

Example

function binSearchRecursive(arr, left, right, target) {
    if (left > right) {
        return -1; // Element not found
    }
    
    let mid = left + Math.floor((right - left) / 2);
    
    if (arr[mid] === target) {
        return mid;
    } else if (arr[mid] 

Given array: 2 4 6 8 10
Target element: 6
Element 6 found at index: 2

Comparison of Approaches

Approach Time Complexity Space Complexity Advantages
Iterative O(log n) O(1) Memory efficient, no stack overflow risk
Recursive O(log n) O(log n) More intuitive, cleaner code structure

Key Points

  • Binary search only works on sorted arrays
  • Time complexity is O(log n) for both approaches
  • Iterative approach is more memory-efficient with O(1) space complexity
  • Always use Math.floor((right - left) / 2) to avoid integer overflow
  • Return -1 to indicate element not found

Conclusion

Binary search is an efficient algorithm for searching sorted arrays with O(log n) time complexity. The iterative approach is preferred for memory efficiency, while the recursive approach offers cleaner, more readable code.

Updated on: 2026-03-15T23:18:59+05:30

864 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements