indexOf();方法

1、indexof方法

indexof方法可以在字符串和数组上使用。

 

2、字符串使用

indexOf(); 方法可返回某个指定的字符串值在字符串中首次出现的位置。

 

//字符串使用
        var s = 'abc';
        console.log(s.indexOf('a'));// 0
        console.log(s.indexOf('b'));// 1
        console.log(s.indexOf('c'));// 2

 

3数组使用

数组使用indexof方法时的兼容情况,ie9以下浏览器不支持。

 

//数组使用
        var arr = ["feng","2019","c"];
        console.log(arr.indexOf("feng"));// 0
        console.log(arr.indexOf("2019"));// 1
        console.log(arr.indexOf("c"));// 2

 

posted @ 2019-07-12 15:39  小菜鸡的春天  阅读(1739)  评论(0)    收藏  举报