Why doesn’t jQuery bomb if your selector object is invalid?

Think of it as a query, which it is. You are asking for all “records” (DOM elements) that match your criteria. The result is a set of zero records.

It then loops over your zero records and applies the action to them. 🙂

If you did the same thing with SQL, or an array, it would behave the same way in most languages. A collection of zero records is not an error state.

var things = $("invalid selector");
$("p").text("The object is valid: " + things + " but has " + things.length + " elements.")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p></p>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)