includes是EJS6新语法,
indexOf返回的是找到返了对应的索引值,找不到返回-1
includes返回true,false,空值时为undefined
includes 可以查找到NaN,indexOf不能
例如:
var a=['aa','bb','cc','dd','ee',NaN]
console.log(a.indexOf('dd')) //3
console.log(a.indexOf(NaN)) //-1
console.log(a.includes('ee')) //true
console.log(a.includes(NaN)) //true
//这是因为 indexOf 认为稀疏数组,省略掉的值是不存在的,但 includes 认为是undefined
var b=[,,]
console.log(b.indexOf(undefined)) //-1
console.log(b.includes(undefined)) //true
//如果是数组时arr.includes(searchElement, fromIndex) /arr.indexOf(searchElement, fromIndex)
从arr.length+fromIndex 的索引值开始查找 (索引值从0开始)
var arr=['a','b','c','d']
console.log(arr.indexOf('a',-1)) // -1
console.log(arr.indexOf('d',-1)) // 3
console.log(arr.includes('a',-1)) //false
console.log(arr.includes('d',-1)) //true
//如果是字符串时str.includes(searchElement, fromIndex) /str.indexOf(searchElement, fromIndex)
从fromIndex 的索引值开始查找
var str='abcde'
console.log(str.indexOf('d',-1)) //3
console.log(str.includes('a',-1)) //true