Check If jQuery is Loaded Using typeof Operator in JavaScript


jQuery is the most used and popular library of JavaScript. If jQuery isn’t loaded, a jQuery coded site will not function properly. Such sites will reload automatically or give an error when jQuery isn’t loaded. But, how do they check if jQuery is loaded ? You will find a simple solution to accomplish this task in this post. Since, jQuery won’t be loaded, we can’t check if jQuery is loaded using jQuery. But we can use JavaScript which will always be available in browsers.

In JavaScript, there is an operator called typeof, which will return the type of a variable. We are going to pass the jQuery global function which is $ to typeof, if it returns the string “function”, then jQuery is loaded. Here is a simple example :

if(typeof($)==”function”){

// jQuery is loaded

}else{

// jQuery isn’t loaded

}

The above code isn’t perfect, because there are other libraries which has the global function $. So, we have to check if jQuery is a function too :

if(typeof($)==”function” && typeof(jQuery)==”function”){

// jQuery is loaded

}else{

// jQuery isn’t loaded

}

The above code is perfect if your site don’t have a custom function named jQuery.