JavaScript indexof()方法用于查找作为函数参数提供的元素的索引。
- indexof()方法区分大小写。
indexof - 语法
Array.indexof(value, start)
indexof - 参数
value - 要在索引中搜索的值。
start - 默认为0。从哪个位置开始搜索。
indexof - 返回值
它返回搜索元素的索引。如果在数组中找不到该元素,则此方法返回-1。
indexof - 浏览器支持
| Chrome | 是 |
| Edge | 是 |
| Firefox | 是 |
| Opera | 是 |
indexof - 例子1
JavaScript TypedArray indexOf()方法
<script type="text/javascript"> // JavaScript to illustrate indexOf() method function JavaTpoint() { var array = [2,3,5,4,6,7,8,9,12]; document.write(array.indexOf(6, 5)); } JavaTpoint(); // expected output: arr[Output:-1] </script>
输出:
-1
indexof - 例子2
JavaScript TypedArray indexOf()方法
<script type="text/javascript"> // JavaScript to illustrate indexOf() method function JavaTpoint() { var array = [2,3,5,4,6,7,8,9,12]; document.write(array.indexOf(6, 2)); } JavaTpoint(); // expected output: arr[Output:4] </script>
输出:
4