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.

Working of Binary Search

Below is an animation of working of binary search in a sorted array to find the element 42:

Binary Search Animation

Here are some example scenarios of searching an element using binary search:

Scenario 1

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

Scenario 2

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

Below are the steps to search for an element using binary search with iterative approach:

  • Define a binSearch() function that accepts the array, the size of the array, and the target that you want to search.
  • Then calculate the middle index of the array.
  • Compare the target with the middle element using an if-else statement. Return the middle element if the target is found at the middle index.
  • If the target is not available at the middle index, then check if it is greater or less than the middle element.
  • If the target is greater than the middle element, then set the left index to mid + 1 and search in the right half of the array.
  • If the target is less than the middle element, then set the right index to mid - 1 and search in the left half of the array.
  • Repeat the above four steps until the left index becomes greater than the right index. If the element is found, return the index otherwise, return element not found.

Example

Here is an example code implementing the above-mentioned steps to implement binary search using iteration.

function binSearch(arr, n, target) {
   let left = 0, right = n - 1;
   while (left <= right) {
      let mid = left + Math.floor((right - left) / 2);
      if (arr[mid] === target)
         return mid;
      else if (arr[mid] < target)
         left = mid + 1;
      else
         right = mid - 1;
   }
   return -1; 
}

let arr = [1, 3, 5, 7, 9];
let n = arr.length;
let target = 7;
console.log("Given array: " + arr.join(" "));
console.log("Target Element to search: " + target);
let result = binSearch(arr, n, target);
console.log("Element " + target + " found at index: " + result);

The output of the above code is as follows:

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

Binary Search Using Recursion

The following are the steps to implement binary search using recursion:

  • Define a binSearch() function that accepts the array, the left index, the right index, and the target that you want to search.
  • Return element not found if the left index becomes greater than the right index.
  • Then, calculate the middle index of the array and compare the target with the middle element using an if-else statement. Return the middle index if the target is found at the middle index.
  • If the target is not available at the middle index, then check if it is greater or less than the middle element.
  • If the target is greater than the middle element, then recursively call the binSearch() function by setting the left index as mid + 1 and searching in the right half of the array.
  • If the target is less than the middle element, then recursively call the binSearch() function by setting the right index as mid - 1 and searching in the left half of the array.
  • Repeat the above four steps until the left index becomes greater than the right index. If the element is found, return the index otherwise, return element not found.

Example

Following is an example code implementing the above-mentioned steps to search for 6 in the given array with binary search using recursion.

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

let arr = [2, 4, 6, 8, 10];
let n = arr.length;
let target = 6;
console.log("Given array: " + arr.join(" "));
console.log("Target Element to search: " + target);
let result = binSearch(arr, 0, n - 1, target);
console.log("Element " + target + " found at index: " + result);

The output of the above code is as follows:

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

Complexity Comparison

Here is a comparison of the time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Iterative Technique O(log n) O(1)
Recursive Approach O(log n) O(log n)
Updated on: 2025-08-21T19:19:04+05:30

743 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements