js判断不等于-1的意思

今天看到这个这样一段代码

if(cc.charCodeAt(i)>10000&&ss.indexOf(cc.charAt(i))!=-1)

不是很理解,这里判断是不是等于-1有什么意义。布尔值不是只有0和1吗。。

阅读 9.3k
11 个回答

indexOf返回所在位置,-1表示没有找到

感觉楼主没理清 括弧的层次关系。。。
if(cc.charCodeAt(i)>10000&&ss.indexOf(cc.charAt(i))!=-1)

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置
如果没有找到匹配的字符串则返回 -1

你需要找的是api

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.

indexOf,查找的时候如果返回-1,就代表没有你要找的

indexOf是按索引找元素的 如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1
索引一般是从0开始的 所以-1表示没有找到
在这里 跟布尔值没有关系

indexOf 查找字符串,如果找不到就返回-1,如果找到就返回值的下下标,也就是index的值

indexOf返回查找字符串所在位置,0是起始位置,-1表示没有找到

clipboard.png

推荐问题