304 North Cardinal St.
Dorchester Center, MA 02124

# How do I examine if an Array features a worth in JavaScript?

On this article, we’re going to discover ways to examine if a worth is current in an array or not. To take action we would require the array to look in and the goal component whose presence needs to be checked.

JavaScript Arrays are used to retailer an inventory of parts that may be accessed by a single variable.

As soon as we’ve got a goal component we will carry out any of the search algorithms to examine for the presence of the component within the array.

## 1. Linear Search Algorithm (Naive method)

Within the Linear search algorithm, we examine every component of the array with the goal. 8 is a part of the num array beneath.

## Javascript

 `var` `num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];` ` `  `operate` `examine(component) {` ` `  `    ``for` `(``var` `i = 0; i < num.size; i++) {` `        ``if` `(num[i] == component)` `            ``return` `component + ``" is current within the array."``;` ` `  `    ``}` `    ``return` `component + ``" isn't current within the array."``;` `}` `console.log(examine(8));`
Output

`8 is current within the array.`

## 2. Utilizing indexOf() operate

The indexOf() operate returns the index of the goal component within the array whether it is current and -1 if not current.

For instance, 41 isn’t a part of the num array within the code beneath.

## Javascript

 `var` `num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];` `var` `component = 41;` `if` `(num.indexOf(component) > 0)` `    ``console.log(component + ``" is current."``);` `else` `    ``console.log(component + ``" isn't current."``);`

## 3. Binary Search

The Binary search algorithm works solely on sorted arrays and retains dividing the array into 2 equal halves and works recursively.

## Javascript

 `operate` `bsearch(arr, l, r, x) {` `    ``if` `(r >= l) {` `        ``let mid = l + Math.flooring((r - l) / 2);` ` `  `        ``if` `(arr[mid] == x)` `            ``return` `mid;` ` `  `        ``if` `(arr[mid] > x)` `            ``return` `bsearch(arr, l, mid - 1, x);` ` `  `        ``return` `bsearch(arr, mid + 1, r, x);` `    ``}` ` `  `    ``return` `-1;` ` `  `}` ` `  `var` `num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];` ` `  `console.log(``"Is 85 current? "``+(bsearch(num, 0, num.size, 85) != -1));` ` `  `console.log(``"Is 1 current? "``+(bsearch(num, 0, num.size, 1) != -1));`
Output

```Is 85 current? false
Is 1 current? true```