Binary search is a very powerful algorithm for finding results on datasets of any size. Though, with such performance comes also one important required for it to work properly: the list **must** be sorted. Another fundamental requirement is knowing the number of items within said list, but in JavaScript-land this is a given.

### How it works

The mechanism is quite simple actually: the `array`

is broken into 2 halves, which leads the program with 2 range of sorted values (already half of the size of initial set). Now, the program checks into which range of values the target value belongs.And, subsquentially it executes again until the searched value is on either end of the `array`

.

### The code

Let's see how a Binary Search could be implemented in Typescript:

`list`

: the sorted`array`

.`value`

: the target value to be found.`start`

: the`index`

in which the current iteration shall commence.`stop`

: the`index`

in which the current iteration shall terminate.`middle`

: the`index`

which represents the exact half of the current list.`@return`

: the index of the found target

tsconst binarySearch = (list: number[],value: number,start = 0,stop: number):number {// `start` can be defined, but will default to `0`// as it is the first defined value of any array// if undefined, `stop` becomes the index of the last itemstop = stop || list.length - 1// `middle` is the mean between `start` and `stop`const middle = Math.floor((start + stop) / 2)// if middle is not the target valueif (list[middle] === value) return middleif (value < list[middle]) {// defines the `stop` value for the next iterationstop = middle - 1} else {// defines the `start` value for the next iterationstart = middle + 1}// binarySearch calls itself again with the newly set valuesreturn binarySearch(list, value, start, stop)}

## Shameless plug

I have a personal playground where I plan to implement some Computer Science basics in JavaScript. With good test coverage and some insights on when/why to use each concept. Check out my insertion sort implementation at the FUNdamentals repository.