Using regular JavaScript, I can do the following:
var ninjaTurtle = document.getElementById('raphael') || document.getElementById('leonardo');
If the first DOM search returns null , which is false, the second search is evaluated.
The jQuery $() function always returns an object of type jQuery, similar to an array, even if no elements are matched. Even if this object is empty, this object is not false, so the following expression will never evaluate the right side:
var ninjaTurtle = $('#raphael') || $('#leonardo');
So what is the idiomatic way to do this kind of fallback when using jQuery?
source share