Can someone explain the difference between using a $ when defining variables in jQuery and not. Does it have any impact on performance?
var $someVar = $("a");
var someVar = $("a");
Also, what is the difference in calling variables with and without $(someVar) For example:
$(someVar).html();
someVar.html();
In your first snippet, there is no difference between those two. It’s just a “
notification” that this variable is holding awrappet setof jQuery objects, which is commonly used by some developers.In your second snippet you are actually doing two different things. You are wrapping
someVarinto a jQuery object and then access ajQuery method(html()).In this example
someVarcould contain aDOM elementor aselector stringlike “#some_element_id”.The other line assumes that
someVaralready IS a jQuery object, otherwise this call would fail.