最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery inArray()'s return values - Stack Overflow

programmeradmin0浏览0评论

I was reading jQuery's documentation about the inArray method and I can't really understand the meaning of one sentence. After saying that the method returns -1 if the element is not in the array, and 0 otherwise, it reads:

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

I can't understand what the fact that JavaScript treats 0 as loosely equal to false has to do with the need to check the method's return values. Would't I need to do this even if JavaScript didn't work this way?

I was reading jQuery's documentation about the inArray method and I can't really understand the meaning of one sentence. After saying that the method returns -1 if the element is not in the array, and 0 otherwise, it reads:

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

I can't understand what the fact that JavaScript treats 0 as loosely equal to false has to do with the need to check the method's return values. Would't I need to do this even if JavaScript didn't work this way?

Share Improve this question asked May 7, 2012 at 8:46 user1301428user1301428 1,7833 gold badges26 silver badges58 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 8

It basically means that you need to write the test like this:

if ($.inArray(value, array) > -1) {
    // the element is inside the array
}

instead of:

if ($.inArray(value, array)) {
    // the element is inside the array
}

The reason why you shouldn't use the second construct is because if the value is the first element of the array the inArray function will return 0 (because that's the index of the first element of an array) and as explained in the documentation 0 means false:

if (0) {
    // this will never run
}

They're just covering their backsides over the hideous mis-naming of that function1.

In other words, they're saying that you mustn't do:

if ($.inArray(a, b) == false) {
   // will fail if the element is in index 0
}

1 A function named inArray would logically (no pun intended) be expected to return a boolean, not a position.

It just means inArray should have really been named indexInArray.

This makes it so that a 0 means it's been found in the first element, and not that it wasn't found.

Indexes start with 0, so 0 is a valid value in the case like

​var arr = ['foo']​​;
​console.log($.inArray('foo', arr));​​​​​

So jQuery developers decided to use -1 instead of false to prevent obvious errors in case if developer checks the value presence like

if (!$.inArray(..))

In javascript 0 is false and 1 is True, or you can use true or false. zero is a position of the array is the first item of the array.

if you compare in the if statment if(0) it is considered has false.

but your answer in the context of the array is that the item is in the zero position.

They tell you don't check for 0 is it's like false or null etc'
it will be a problem if the value is in index 0 of the array(== false)

Check if the value is greater than -1 or !=-1

Example of the problem:

if ($.inArray(20, [20]))
    // won't run the value is in index - 0

if ($.inArray(20, [20]) > -1)
    // Will run!

DEMO

发布评论

评论列表(0)

  1. 暂无评论