Why does JQuery have dollar signs everywhere?
Solution 1:
$
is just a shortcut for jQuery
. The idea is that everything is done with the one global symbol (since the global namespaces is ridiculously crowded), jQuery
, but you can use $
(because it's shorter) if you like:
// These are the same barring your using noConflict (more below)
var divs = $("div"); // Find all divs
var divs = jQuery("div"); // Also find all divs, because
console.log($ === jQuery); // "true"
If you don't want to use the alias, you don't have to. And if you want $
to not be an alias for jQuery
, you can use noConflict
and the library will restore $
to whatever it was before jQuery took it over. (Useful if you also use Prototype or MooTools.)
Solution 2:
$
sign is an alias for jQuery
. A short version of jQuery
, a less write mechanism.
Just for an example: (in jQuery it's more complicated)
var yourFunction = function() {
alert('a function');
}
window.Myf = yourFunction;
Now you can call yourFunction
like:
Myf(); // definitely a short syntax
Solution 3:
It's just a convenient character, shorter to type and easier to read than "jQuery".
There is nothing special except that it's traditionally not used to start a variable or function name, which reduces the risk or name collision.
Solution 4:
Writability and Performance
When we are working on library or a programming language we should pay attention to some writability rules. Thanks to jQuery they already implemented lots of options. You can use $
or you can use jQuery
or you can use _
(function (_) {
_("#wow").click()
})(jQuery);
Or maybe you can do fancy changes, javascript identifiers are unicode so you can use Ω
(function (Ω) {
Ω("#wow").click()
})(jQuery);
But the main idea behind it, pressing once to the keyboard is better than typing jQuery
On the other side, we have performance... I just randomly opened one of my projects and searched for $
, I used 54 $
in a single javascript file.
$
is a byte.
jQuery
is 6 bytes.
The difference is huge 54 * 5 = 220 bytes.
Solution 5:
Google is your friend: $ sign JQuery
Dollar Sign is just an alias for JQuery.
jQuery(document).ready(function(){});
OR
$(document).ready(function(){});