Binary Search in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the fundamentals of JavaScript programming. We will cover topics such as variables, data types, functions, and control flow. By the end of the lab, you will have a solid understanding of JavaScript syntax and be able to write basic programs using this language. This lab is designed for beginners with little to no programming experience, but it will also be useful for those who want to refresh their knowledge of JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") subgraph Lab Skills javascript/variables -.-> lab-28173{{"`Binary Search in JavaScript`"}} javascript/data_types -.-> lab-28173{{"`Binary Search in JavaScript`"}} javascript/arith_ops -.-> lab-28173{{"`Binary Search in JavaScript`"}} javascript/comp_ops -.-> lab-28173{{"`Binary Search in JavaScript`"}} javascript/cond_stmts -.-> lab-28173{{"`Binary Search in JavaScript`"}} javascript/loops -.-> lab-28173{{"`Binary Search in JavaScript`"}} javascript/array_methods -.-> lab-28173{{"`Binary Search in JavaScript`"}} end

To begin coding practice, open the Terminal/SSH and type node. The binary search algorithm is used to find the index of a given element in a sorted array. Here are the steps to implement the binary search algorithm:

  1. Declare the left and right search boundaries, l and r, initialized to 0 and the length of the array respectively.
  2. Use a while loop to repeatedly narrow down the search subarray by dividing it in half using Math.floor().
  3. If the element is found, return its index. Otherwise, return -1.
  4. Note that this algorithm does not account for duplicate values in the array.

Here's an example implementation of the binary search algorithm in JavaScript:

const binarySearch = (arr, item) => {
  let l = 0,
    r = arr.length - 1;
  while (l <= r) {
    const mid = Math.floor((l + r) / 2);
    const guess = arr[mid];
    if (guess === item) return mid;
    if (guess > item) r = mid - 1;
    else l = mid + 1;
  }
  return -1;
};

You can test the binarySearch function with the following examples:

binarySearch([1, 2, 3, 4, 5], 1); // 0
binarySearch([1, 2, 3, 4, 5], 5); // 4
binarySearch([1, 2, 3, 4, 5], 6); // -1

Summary

Congratulations! You have completed the Binary Search lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like