JavaScript: Binary search using recursion
JavaScript Function: Exercise-8 with Solution
Binary Search
Write a JavaScript program for binary search.
Sample array : [0,1,2,3,4,5,6]
console.log(l.br_search(5)) will return '5'
Visual Presentation:

Sample Solution-1:
JavaScript Code:
Output:
5
Flowchart:

Live Demo:
Sample Solution-2:
JavaScript Code:
Output:
The target value 5 is found at index 5.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a JavaScript function that implements binary search iteratively on a sorted array.
- Write a JavaScript function that performs recursive binary search and returns the index of the found element.
- Write a JavaScript function that applies binary search on a sorted array of objects based on a specified key.
- Write a JavaScript function that implements binary search and returns the index of the first occurrence when duplicates exist.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript program to check whether a number is even or not.
Next: Write a merge sort program in JavaScript.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.