Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JQuery have dollar signs everywhere?

Tags:

jquery

People also ask

Why is jQuery a dollar sign?

The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector. It is given a shorter identifier as $ just to reduce the time for writing larger syntax.

What does '$' mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What is '$' in jQuery?

$ is pretty commonly used as a selector function in JS. In jQuery the $ function does much more than select things though. You can pass it a selector to get a collection of matching elements from the DOM. You can pass it a function to run when the document is ready (similar to body.

Can we replace dollar sign in jQuery?

noConflict() "frees" the "$" from being associated with jQuery. Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict() you can't do that anymore and so you have to replace each "$" with "jQuery"; .


$ 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.)


$ 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

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.


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.