In jQuery, what is the difference between $
and $.
? Please provide a practical explanation if possible.
$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.
The “dollar sign function” $() is typically used to override the lengthy DOM function document. getElementById().
$
is a reference (or synonym, if you want an analogy) to the global jQuery
object. $.<method>
calls a static method of the jQuery
object, where $(<selector>)
creates a new instance of the jQuery
object.
In the context of jQuery, $
refers to the global jQuery
object.
$.
by itself is invalid JavaScript. As $
is an object, methods on this object are called like on any other object: $.methodname()
.
Maybe it becomes clearer by substituting $
with jQuery
: jQuery.methodname()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With