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