Testing for a jQuery object in Javascript

I’ve just spent an inordinate amount of time trying to figure out how to test if an object in Javascript has actually been extended using jQuery or not. Ok, not actually that long; but probably longer than it really should have taken, and I had to dig deep into the bowels of the internet to find the answer.

Furthermore, the answer is pretty simple really:

if(obj instanceof jQuery)  

It does what it says on the tin – if the object is an instance of jQuery, i.e. you are dealing with a jQuery-enhanced object, then do something. This can be useful if you have a function which expects a jQuery object and want to make sure that the object has been jQuery-ified. Or, conversely as in my case and the reason for this post, you want to ensure that you’re dealing with a plain Html node:

if(myElement instanceof jQuery) {  
   myElement = myElement[0]; 
} // .. continue