I recently wrote a small script that made a simple show/hide manipulation to an unordered list.
From my understanding, jQuery will only execute a function if the element in question exists (therefore if it’s ‘true’). So there is no practical reason in doing a check to see if the length of the element is bigger than 0, if it has a parent node which is body and so on. Correct?
If the above is accurate, when would actually such a check be helpful?
If the action you want to perform could cascade into errors if an element doesn’t exist, it would be helpful to do a check first. Something fairly trivial (like show/hide) probably wouldn’t warrant a check.
(Although it’s good programming practice to make sure everything works as intended, and fails gracefully. A check would help with that, or at least a try… catch statement.)