Understanding JavaScript Array Series XV - Array Loops & Iteration Part XII
Tags: javascript, webdev, beginners
Published: 2019-10-17 21:28:19

In the last two articles, I talked about iterating over arrays using the `Array.indexOf()` and the `Array.lastIndexOf()` array method. You can check it out below:

{% link https://dev.to/nedyudombat/understanding-javascript-array-series-xiii-array-loops-iteration-part-x-1obm %}

{% link https://dev.to/nedyudombat/understanding-javascript-array-series-xiv-array-loops-iteration-part-xi-32no %}

The `Array.indexOf()` and the `Array.lastIndexOf()` both return the index when the element being searched for exists in the array and the `-1` if it doesn't. What if we want it to return a boolean? :thinking: That returns true when the element we are searching for exists and false when it doesn't? Well you are in luck, today we will discuss the `Array.includes()` method.

## **Array.includes()**
  This method checks for a particular element in an array, if the element is found it returns true else it returns false.

It's syntax is similar to that of the `Array.indexOf()`, save for some differences:
```javascript
// syntax
arr.includes(element, startIndex);
```
**[element]:** This is the element that will searched for in the array.

**[startIndex]:** This is the position(index) of the array to begin the search from. If this value is not supplied it defaults to `0`. If the provided index is negative starts the search from `arr.length + (startIndex)`.

Some Examples: 
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];

console.log(names.includes('nedy')) // true
console.log(names.includes('nedy', 6)) // false
```

**What happens if the `startIndex` is equal to or greater than the length of the array?**
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];

console.log(names.includes('nedy', 6)) // false
console.log(names.includes('nedy', 30)) // false
```
In this scenario the array is not searched.

**What happens if the calculated startIndex is less than 0?**
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];

console.log(names.includes('nedy', -30)) // true
console.log(names.includes('nedy', -8)) // false
```
In the first scenario, the calculated startIndex will be `6 + (-30)` which is `-24` it returns true, but in the second scenario where the calculated index is `6 + (-8)` which is `-2` it returns true. This is because for `Array.includes()` if the calculated startIndex is less than or equal to `-1 * arr.length`(which is `-6` in this case), the entire array will be searched. In this situation, the first scenario `-24` is less than `-6` so the array will be searched, but in the second scenario `-2` is greater than `-6`, hence, the array will not be searched.

**What happens if `Nedy` or `neDy` is being searched for?**
```javascript
const names = ['soji', 'nedy', 'naza', 'chukwudi', 'lii', 'nedy'];

console.log(names.includes('Nedy',)) // false
console.log(names.includes('neDy', 3)) // false
```
The reason is simple `Array.includes()` is case sensitive.


## **Conclusion**
  `Array.includes()` should be used when you want to check if an element is present in an array. If you want to check for the position of the element in an array you can use `Array.indexOf()`, I wrote about it [here](https://dev.to/nedyudombat/understanding-javascript-array-series-xiii-array-loops-iteration-part-x-1obm)

That's all for today, tomorrow we will talk about another set of functions used in array Iteration.

Here is the link to the other articles on this Array series written by me:
* [What is an Array?](https://dev.to/nedyudombat/understanding-javascript-array-series-i-what-is-an-array-l1i)
* [Alternate ways of Creating an Array.](https://dev.to/nedyudombat/understanding-javascript-array-series-ii-alternate-ways-of-creating-an-array-26c8)
* [Array Properties](https://dev.to/nedyudombat/understanding-javascript-array-series-iii-array-properties-43co)
* [Array Loops & Iteration Part I] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-i-334j)
* [Array Loops & Iteration Part VII `(find())`] (https://dev.to/nedyudombat/understanding-javascript-array-series-x-array-loops-iteration-part-vii-18dc)
* [Array Loops & Iteration Part VIII `(findIndex())`] (https://dev.to/nedyudombat/understanding-javascript-array-series-xi-array-loops-iteration-part-viii-17gp)

Got any question, addition or correction? Please leave a comment.

Thank you for reading. :thumbsup: