# 章序号/节序号/节/笔记序号 codecademy,array # 概念阐释 - 显示元素的索引位置 ## 语法 ```js array.indexOf(element, startIndex); ``` - `element` 要寻找位置对值 - 开始搜索的可选 `startIndex` 位置。 如果没有给出,则搜索从数组的开头开始。 负索引将从数组的末尾偏移。 # 举例子 ```js const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains']; const pastaIndex = groceryList.indexOf('pasta'); console.log(pastaIndex);//print 4,没有结果会print -1 ``` - `console.log( groceryList.indexOf('pasta'));` 的结果和上面一样。 - **与push不同**,如果按照.push的写法就不能得到正确结果了: ```js groceryList.indexOf('pasta'); console.log(groceryList); ``` # 类比、比较与对比 和[[js Iterators 数组函数内置对象findIndex()]] 的结果一样,区别在于findIndex可筛选的方式更多? # 问题 ```js const colors = ['blue', 'orange', 'pink', 'yellow', 'teal']; const checkBlue = colors.indexOf('pink', 3); console.log(checkBlue); ``` - 查找pink的位置,从头开始搜索 - 查找yellow的位置,从index2开始搜索 - 查找pink的位置,从它后面开始搜索,会得出什么结果? # 问题答案 # 备注(经验集/错误集) ## 经验集 ## 错误集 ## 参考资料 [`.indexOf()` in Docs](https://www.codecademy.com/resources/docs/javascript/arrays/indexOf).