{"id":20076,"date":"2020-07-24T02:56:29","date_gmt":"2020-07-24T09:56:29","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=20076"},"modified":"2023-12-01T03:56:02","modified_gmt":"2023-12-01T11:56:02","slug":"binary-search-javascript","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/","title":{"rendered":"Binary Search JavaScript: A Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">How to Code a Binary Search in JavaScript<\/h2>\n\n\n\n<p>Searching algorithms makes life so much easier as a programmer. Doing so makes it easy to find a particular item inside a data set of tens, hundreds, or thousands of items.<br><\/p>\n\n\n\n<p>One of the most popular forms of searches is the binary search. This search quickly finds an item in an array. Every time the search looks through an item, it reduces the number of items that are left to be searched by half.<br><\/p>\n\n\n\n<p>In this guide, we\u2019re going to talk about what binary searches are and how they work. We\u2019ll then go on to implement a binary search using two different approaches: iterative and recursive.<br><\/p>\n\n\n\n<p>Let\u2019s build a binary search algorithm in <a href=\"https:\/\/careerkarma.com\/blog\/what-is-javascript\/\">JavaScript<\/a>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Binary Search?<\/h2>\n\n\n\n<p>A binary search is a computer science algorithm that searches for an item in a sorted array.<br><\/p>\n\n\n\n<p>It starts in the middle of an array and checks whether the middle item is less than, equal to, or greater than the number for which you are searching.<br><\/p>\n\n\n\n<p>If the number is smaller, the algorithm knows to keep searching in the left half of the array, where the smaller numbers are; if the number is larger, the algorithm will focus on the right half of the array. Binary searches only work on sorted lists.<br><\/p>\n\n\n\n<p>Binary searches are more efficient than linear searches. This is because every time a search is made the number of items left to search in the list is reduced by half.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use a Binary Search<\/h2>\n\n\n\n<p>A binary search is easy to understand once you get the hang of it.<br><\/p>\n\n\n\n<p>Before we implement a binary search algorithm, let\u2019s walk through one step-by-step. We\u2019re going to find the number \u201c9\u201d in a list. Let\u2019s start with a sorted list:<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>2<\/td><td>6<\/td><td>8<\/td><td>9<\/td><td>10<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p>First, we need to find the middle number and assign it to a variable. This is found by calculating the sum of the first and last numbers and dividing it by two. We\u2019ll call this variable \u201cmiddle\u201d:<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td>Start<\/td><td><br><\/td><td><strong>Middle<\/strong><\/td><td><br><\/td><td>End<\/td><\/tr><tr><td>2<\/td><td>6<\/td><td><strong>8<\/strong><\/td><td>9<\/td><td>10<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p>8 is our middle number. Then, we can compare the middle number to the one for which we are searching. If the middle number is equal to the one we are searching for, our search can stop.<br><\/p>\n\n\n\n<p>In this example, 8 is not equal to 9. Our search continues.<br><\/p>\n\n\n\n<p>Next, we need to compare whether the middle number is greater than 9. It is not.<br><\/p>\n\n\n\n<p>This tells us that the number for which we are searching must be after the middle number. 9 is greater than 8 in a sorted list. We are going to set our starting number to be equal to the middle number. This is because we know that the number for which we are searching does not come before the middle number.<br><\/p>\n\n\n\n<p>If the number for which we are searching is smaller, we would set the end number to be equal to the middle number. Because the number is smaller than the middle number, we could focus our search on the lower half of the list.<br><\/p>\n\n\n\n<p>The binary search repeats again on the top half of the list because 9 is greater than 8:<br><\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><br><\/td><td><br><\/td><td>Start<\/td><td><strong>Middle<\/strong><\/td><td>End<\/td><\/tr><tr><td>2<\/td><td>6<\/td><td>8<\/td><td><strong>9<\/strong><\/td><td>10<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n\n\n\n<p>We find the middle number again. This is 9. We can compare 9 to the number for which we are searching. 9 is equal to the number for which we are searching.<br><\/p>\n\n\n\n<p>This means our search can stop. We\u2019ve successfully found the number 9 in our list!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Implement a Binary Search in JavaScript<\/h2>\n\n\n\n<p>Binary searches can be implemented using an iterative or recursive approach.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Iterative Binary Search<\/h3>\n\n\n\n<p>An iterative binary search uses a while loop to find an item in a list. This loop will execute until the item is found in the list, or until the list has been searched.<br><\/p>\n\n\n\n<p>Let\u2019s start by writing a <a href=\"https:\/\/careerkarma.com\/blog\/how-to-use-javascript-functions\/\">function<\/a> that performs our binary search:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function binarySearch(array, numberToFind) {\n\tlet start = 0;\n\tlet end = array.length - 1;\n\n\twhile (start &lt;= end) {\n\t\tlet middle = Math.floor((start + end) \/ 2);\n\n\t\tif (array[middle] === numberToFind) {\n\t\t\treturn middle;\n\t\t} else if (array[middle] &lt; numberToFind) {\n\t\t\tstart = middle + 1;\n\t\t} else {\n\t\t\tend = middle - 1;\n\t\t}\n\t}\n\n\treturn -1;\n}<\/pre><\/div>\n\n\n\n<p>We start by defining two variables: start and end. These keep track of the highest and lowest values our search is working with. We use a <a href=\"https:\/\/careerkarma.com\/blog\/javascript-while-loop\/\">while loop<\/a> that runs until the start number is greater than the end number. This loop calculates the middle number between start and end in the list.<br><\/p>\n\n\n\n<p>If the number for which we are searching is equal to the middle number, the middle number is returned to our main program. If the number is smaller, the start value is set to be equal to the middle number plus one. These comparisons are performed using an <a href=\"https:\/\/careerkarma.com\/blog\/javascript-if-else\/\">if statement<\/a>.<br><\/p>\n\n\n\n<p>Otherwise, the end number is set to be equal to the middle number minus one. If our number is not found after the while loop has run, -1 is returned. We call this a base condition. In our main program, we\u2019ll check if the number returned is equal to -1. If it is, it means our number could not be found.<br><\/p>\n\n\n\n<p>Our function does not work just yet. We need to write a main program that calls it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>let numbers = [2, 6, 8, 9, 10];\nlet toFind = 9;\nlet findNumber = binarySearch(numbers, toFind);\n\nif (findNumber !== -1) {\n\tconsole.log(`${toFind} has been found at position ${findNumber}.`);\n} else {\n\tconsole.log(`${toFind} has not been found.`);\n}<\/pre><\/div>\n\n\n\n<p>We\u2019ve defined a list of numbers through which to search and the number that we want to find in our list. Next, we have called the binarySearch function. This will perform our search. The search will return either -1 or the position of the item for which we are searching.<br><\/p>\n\n\n\n<p>-1 denotes that an item could not be found. If an item is not found, the contents of our <code>else<\/code> statement are executed. Otherwise, the contents of the <code>if<\/code> statement are run.<br><\/p>\n\n\n\n<p>Let\u2019s run our code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>9 has been found at position 3.<\/pre><\/div>\n\n\n\n<p>This tells us that our search has been successful!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recursive Binary Search<\/h3>\n\n\n\n<p>A recursive binary search is considered more elegant than an iterative one. This is because binary searches perform the same operation over and over again on a list. This behavior can be implemented using a recursion algorithm.<br><\/p>\n\n\n\n<p>Open up a new JavaScript file and paste in this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>function binarySearch(array, numberToFind, start, end) {\n\tif (start =&gt; end) {\n\t\treturn -1;\n\t}\n\n\tlet middle = Math.floor((start + end) \/ 2);\n\n\tif (array[middle] === numberToFind) {\n\t\treturn middle;\n\t} else if (array[middle] &lt; numberToFind) {\n\t\treturn binarySearch(array, numberToFind, middle + 1, end);\n\t} else {\n\t\treturn binarySearch(array, numberToFind, start, middle - 1);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>This code makes the same comparisons as our first search. It checks whether the middle number is equal to, greater than, or less than the number for which we are searching.<br><\/p>\n\n\n\n<p>At the start of our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-use-javascript-functions\/\">function<\/a>, we have used an if statement to check if the start number is greater than the end number. If it is, this means our item could not be found in the list we have specified. We return -1 to the main program if this is the case.<br><\/p>\n\n\n\n<p>If the number we\u2019re looking for is the same as the middle number, the middle number is returned to the main program. If the number we\u2019re looking for is greater or less than the middle number, our binarySearch function is run again. This continues until our item is found.<br><\/p>\n\n\n\n<p>To run this function, we\u2019re going to need to make a change to our main program:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>let numbers = [2, 6, 8, 9, 10];\nlet toFind = 9;\nlet findNumber = binarySearch(numbers, toFind, 0, numbers.length - 1);\n\u2026 <\/pre><\/div>\n\n\n\n<p>We need to pass in two additional parameters: the values of \u201cstart\u201d and \u201cend\u201d. The value of \u201cstart\u201d is equal to 0. The value of \u201cend\u201d is equal to the length of the list minus one.<br><\/p>\n\n\n\n<p>Let\u2019s run our code and see what happens:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>9 has been found at position 3.<\/pre><\/div>\n\n\n\n<p>Our binary search was successful! It uses the same underlying algorithm as the iterative approach. The difference is that the binary search is performed by using a function which calls itself until the item is found or until the list is fully searched, whichever comes first.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Binary searches make it easy to find an item in a list. Every time a search is executed, the number of items left to search in a list is reduced by half. This makes a binary search more efficient than a linear search.<br><\/p>\n\n\n\n<p>Now you\u2019re ready to implement a binary search in JavaScript like an expert!<\/p>\n","protected":false},"excerpt":{"rendered":"How to Code a Binary Search in JavaScript Searching algorithms makes life so much easier as a programmer. Doing so makes it easy to find a particular item inside a data set of tens, hundreds, or thousands of items. One of the most popular forms of searches is the binary search. This search quickly finds&hellip;","protected":false},"author":240,"featured_media":14261,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11933],"tags":[],"class_list":{"0":"post-20076","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"JavaScript","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Binary Search JavaScript: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"A binary search is an efficient way to find an item in a sorted list. On Career Karma, learn how to code a binary search in JavaScript.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search JavaScript: A Guide\" \/>\n<meta property=\"og:description\" content=\"A binary search is an efficient way to find an item in a sorted list. On Career Karma, learn how to code a binary search in JavaScript.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-24T09:56:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T11:56:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-92904.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Binary Search JavaScript: A Guide\",\"datePublished\":\"2020-07-24T09:56:29+00:00\",\"dateModified\":\"2023-12-01T11:56:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/\"},\"wordCount\":1272,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/macbook-pro-92904.jpg\",\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/\",\"name\":\"Binary Search JavaScript: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/macbook-pro-92904.jpg\",\"datePublished\":\"2020-07-24T09:56:29+00:00\",\"dateModified\":\"2023-12-01T11:56:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"A binary search is an efficient way to find an item in a sorted list. On Career Karma, learn how to code a binary search in JavaScript.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/macbook-pro-92904.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/macbook-pro-92904.jpg\",\"width\":1020,\"height\":680,\"caption\":\"A laptop displaying HTML\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/javascript\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Binary Search JavaScript: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Binary Search JavaScript: A Guide | Career Karma","description":"A binary search is an efficient way to find an item in a sorted list. On Career Karma, learn how to code a binary search in JavaScript.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search JavaScript: A Guide","og_description":"A binary search is an efficient way to find an item in a sorted list. On Career Karma, learn how to code a binary search in JavaScript.","og_url":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-24T09:56:29+00:00","article_modified_time":"2023-12-01T11:56:02+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-92904.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Binary Search JavaScript: A Guide","datePublished":"2020-07-24T09:56:29+00:00","dateModified":"2023-12-01T11:56:02+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/"},"wordCount":1272,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-92904.jpg","articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/","url":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/","name":"Binary Search JavaScript: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-92904.jpg","datePublished":"2020-07-24T09:56:29+00:00","dateModified":"2023-12-01T11:56:02+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"A binary search is an efficient way to find an item in a sorted list. On Career Karma, learn how to code a binary search in JavaScript.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/binary-search-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-92904.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/04\/macbook-pro-92904.jpg","width":1020,"height":680,"caption":"A laptop displaying HTML"},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/binary-search-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/careerkarma.com\/blog\/javascript\/"},{"@type":"ListItem","position":3,"name":"Binary Search JavaScript: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/20076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=20076"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/20076\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/14261"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=20076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=20076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=20076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}