Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does .find() always return true?

It seems like jQuery's .find() method always returns true. But that's not really useful because you have do additionally check the length of the returned object to see if it really exists.

Anyone got a good explanation for that behaviour?

like image 522
mas-designs Avatar asked Jan 11 '12 09:01

mas-designs


People also ask

Does find return true or false?

@zero298 The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. So no boolean result.

What does return true mean in Javascript?

returning true or false indicates that whether execution should continue or stop right there. So just an example <input type="button" onclick="return func();" /> Now if func() is defined like this function func() { // do something return false; } the click event will never get executed.

What is the return value for true?

Note that these exceptions are not part of the C language itself, but rather with commonly-used C functions. Show activity on this post. In an if statement, or any case where a boolean value (True or false) is being tested, in C, at least, 0 represents false, and any nonzero integer represents true.


4 Answers

Anyone got a good explanation for that habit ?

If .find() were to return a Boolean value instead of a jQuery object, you could not use it for chaining which is one of the overall goals of jQuery.

like image 116
jensgram Avatar answered Oct 15 '22 05:10

jensgram


Example how to check if any elements where matched:

if ( $('body').find('li').length ) {
    // at least one li was found
} else {
   // no li's where found
}

$() and selector methods like find() and filter() always return a jQuery object. This is so you can chain methods. You could do something like

$('body').find('li').add('<p>')

This finds all list elements in the body, and adds a paragraph to all. If $('body').find('li') would return false because it didn't contain any li's, the add() method would throw an error, because you cannot do false.add().

like image 20
Blaise Avatar answered Oct 15 '22 05:10

Blaise


It doesn't return true. It returns an empty set of elements, which is true if you do ==. You need to check .length and check if 0 elements were returned.

like image 20
Dogbert Avatar answered Oct 15 '22 05:10

Dogbert


.find() method of jQuery returns jQuery object, which could be evaluated to true in some cases. But in fact comparing it strictly (===) with true will fail (the comparison will return false).

This is why you should use strict comparison (=== instead of ==) and check for .length property when counting returned elements (this is true also about Array objects).

It is completely reasonable, as the jQuery object is only a container for elements you have found. It must have jQuery methods (the ones you can call on the result of .find()), thus it must not be a boolean.

like image 24
Tadeck Avatar answered Oct 15 '22 06:10

Tadeck