1 Basic Differences

● Both includes() and indexOf() are used to check whether an array contains certain elements. Includes () returns a Boolean value, and indexOf() returns an index value, or -1 if not

let arr = [1.2.3]
arr.indexOf(0)	// -1
arr.indexOf(2)	/ / 1
arr.includes(2)	// true
Copy the code

2 Check NAN and undefined

● Because indexOf() compares values strictly to the === operator, indexOf() does not check NAN, but includes() does

let arr = [NaN,]
arr.indexOf(NaN)	// -1
arr.indexOf(undefined) // -1
arr.includes(NaN)	// true
arr.includes(undefined)	// true
Copy the code

3 Check -0 and +0

● Includes () and indexOf() do not distinguish between -0 and +0

let arr = [+0]
arr.includes(-0) // true
arr.indexOf(-0) / / 0
Copy the code

4 Cannot check complex data types

● Both can only judge simple data types, objects, arrays and other complex data types are not able to judge

let arr = [{a:1}, {a:2}]
arr.includes({a:1}) // false
arr.indexOf({a:1}) // -1
Copy the code

5 indexOf() can be used for strings

● Returns the position where the specified character first appears, and there is an implicit conversion

let str = 'a1b2c3'
str.indexOf('2')); / / 3
str.indexOf(1)); / / 3
Copy the code