I can see there are many ways to use jQuery safely in the javascript code. That means that the typical ‘$’ shortcut for jQuery doesn’t work, so it doesn’t conflict with any other JavaScript libraries.
-
We can create no conflict object.
var j = jQuery.noConflict(); // Do something with jQuery j("div p").hide(); -
Using jQuery instead on $ in the code.
jQuery("div p").hide(); -
Using Immediately-Invoked Function Expression (IIFE) something like this.
(function($) { $("div p").hide(); })( jQuery );
So which will be the best method to use? Or is there a better method not listed?
Thanks in advance…!!
It is a good practice/habit to use jQuery instead of $. So the ideal way would be method 2. $ is just a shorthand for the word jQuery. Although you can use any of the 3, all three are equally okay