3

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?

6 Answers 6

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
}
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

I agree that it's hideously misnamed. It's clear from the function name that it returns a boolean. And yet it doesn't return a boolean. (Sometimes I want to strangle other developers.) We all need to create our own libraries to include a wrapper function that truly (no pun intended) does return a boolean.
3

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.

Comments

1

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(..))

2 Comments

So instead of using if (!$.inArray(..)) I should use if ($.inArray(..) == -1)?
@user1301428: if (!$.inArray(..)) --- is incorrect, because it would match both 0 and false cases (if jquery developers decided to use false). So, yes, compare it to -1 to check if the value doesn't present in the array
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.