Question: Beyond the improved readability, is there any advantage to `includes` over `indexOf`? They seem identical to me.

What is the difference between this

```
var x = [1,2,3].indexOf(1) > -1; //true

```

And this? 

```
var y = [1,2,3].includes(1); //true

```

Answer: 
`.indexOf()` and `.includes()` methods can be used to search for an element in an array or to search for a character/substring in a given string.

Usage in Array
--------------

([Link](https://www.ecma-international.org/ecma-262/7.0/#sec-array.prototype.includes) to ECMAScript Specification)

1. `indexOf` uses [Strict Equality Comparison](https://www.ecma-international.org/ecma-262/7.0/#sec-strict-equality-comparison) whereas `includes` uses the [SameValueZero](https://www.ecma-international.org/ecma-262/7.0/#sec-samevaluezero) algorithm. Because of this reason, the following two points of differences arise.
2. As pointed out by [Felix Kling](https://stackoverflow.com/users/218196/felix-kling), the behavior is different in case of `NaN`.

```
let arr = [NaN];

arr.indexOf(NaN); // returns -1; meaning NaN is not present
arr.includes(NaN); // returns true

```

3. The behavior is also different in case of `undefined`.

```
let arr = [ , , ];

arr.indexOf(undefined); // returns -1; meaning undefined is not present
arr.includes(undefined); // returns true

```

Usage in String
---------------

([Link](https://www.ecma-international.org/ecma-262/7.0/#sec-string.prototype.includes) to ECMAScript Specification)

1.
If you pass a RegExp to `indexOf`, it will treat the RegExp as a string and will return the index of the string, if found. However, if you pass a RegExp to `includes`, it will throw an exception.

```
let str = "javascript";

str.indexOf(/\w/); // returns -1 even though the elements match the regex because /\w/ is treated as string
str.includes(/\w/); // throws TypeError: First argument to String.prototype.includes must not be a regular expression

```

Performance
-----------

As [GLAND\_PROPRE](https://stackoverflow.com/users/5309984/538romeo) pointed out, `includes` may be a little (very tiny) bit slower (for it needs to check for a regex as the first argument) than `indexOf` but in reality, this doesn't make much difference and is negligible.

History
-------

`String.prototype.includes()` was introduced in ECMAScript 2015 whereas `Array.prototype.includes()` was introduced in ECMAScript 2016. With regards to browser support, use them wisely.

`String.prototype.indexOf()` and `Array.prototype.indexOf()` are present in ES5 edition of ECMAScript and hence supported by all browsers.
The answers and examples were all great. However (at first glance), it made me misunderstood that `includes` will always return `true` when using `undefined`.

Hence I am including the example to elaborate **includes can be used to check for undefined and NaN values** wherelse **indexOf can't**

```js
//Array without undefined values and without NaN values. 
//includes will return false because there are no NaN and undefined values

const myarray = [1,2,3,4]

console.log(myarray.includes(undefined)) //returns false
console.log(myarray.includes(NaN)) //returns false

//Array with undefined values and Nan values.
//includes will find them and return true

const myarray2 = [1,NaN, ,4]

console.log(myarray2.includes(undefined)) //returns true
console.log(myarray2.includes(NaN)) //returns true

console.log(myarray2.indexOf(undefined) > -1) //returns false
console.log(myarray2.indexOf(NaN) > -1) //returns false
```

Summary

* `includes` **can** be used to check for undefined and NaN values in array
* `indexOf` **cannot** be used to check for undefined and NaN values in array