1

参考链接

indexOf()只返回字符串在规定的查找顺序中,首次出现的位置!

1、传参相同

var arr = [0,1,'2'];
var str="Hello world!";
console.log("Array",arr.indexOf(1,0));      //1
console.log("string",str.indexOf('d',0));   //10 

2、返回值类型相同

var arr = [0,1,'2'];
var str="Hello world!";
console.log("Array",arr.indexOf(1));      //1
console.log("string",str.indexOf('s'));   //-1  

3.不同

字符串的indexOf在传入参数不是字符串的情况下默认会转换为字符串

var str="Hello 666!";
console.log("string",str.indexOf('6'));   //6
console.log("string",str.indexOf(6));   //6 

而数组的indexOf不会进行数据类的转换

var arr = [0,1,'2'];
console.log("Array",arr.indexOf(2));      //-1
console.log("Array",arr.indexOf('2'));      //2  

赞链接


yzbao
626 声望19 粉丝

Uncaught ReferenceError