为什么IndexOf返回-1? [英] Why does IndexOf return -1?

查看:724
本文介绍了为什么IndexOf返回-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Javascript并且不明白为什么indexOf返回-1:

I am learning Javascript and don't understand why the indexOf below returns -1:

var string = "The quick brown fox jumps over the lazy dog";

console.log (string.indexOf("good"));


推荐答案

-1表示找不到匹配项。

-1 means "no match found".

它返回-1而不是false的原因是字符串开头的一个针位于位置0,这在Javascript中等于false。所以返回-1确保你知道实际上没有匹配。

The reason it returns -1 instead of "false" is that a needle at the beginning of the string would be at position 0, which is equivalent to false in Javascript. So returning -1 ensures that you know there is not actually a match.

这篇关于为什么IndexOf返回-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆