118

I am working on a project with quite a lot of JQuery in it. The JQuery has a lot of $ signs everywhere, for example

$(document).ready(function () {
        $('input[type=file]').wl_File({
            url: '/Admin/PolicyInventory/UploadDocuments',
            onFileError: function (error, fileobj) {
                $.msg('file is not allowed: ' + fileobj.name, {
                    header: error.msg + ' Error ',
                    live: 10000
                });
            }
        });
...

My question is, what does this dollar sign mean? Why is it used all over the place and how do I understand and interpret it? It reminds me of the scary days of when I was learning Scheme at University and had to put brackets everywhere without knowing why I was doing it.

Sachin Kainth
  • 41,237
  • 78
  • 185
  • 289

7 Answers7

170

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

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
13

$ 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
YakovL
  • 5,213
  • 10
  • 46
  • 71
thecodeparadox
  • 81,835
  • 21
  • 131
  • 160
12

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.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • 3
    Unless you are using another language like PHP to populate javascript in your site using HTMLdocs, at which point that dollar sign serves the additional purpose of breaking everything since PHP *does* use the dollar sign to start all variables. (A reason that if you're sharing jQuery scripts for broad net use, it's kind to use the jQuery instead of $). – lilHar Apr 26 '16 at 21:49
11

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.

Ahmet Can Güven
  • 4,734
  • 4
  • 31
  • 49
  • 1
    If you are making a negative vote, please don't be selfish to tell me what I did wrong. So that I can fix my mistakes if there is any! – Ahmet Can Güven Aug 09 '16 at 13:47
  • 2
    220 bytes is not really going to make any significant difference as far as speed goes. Also, it wouldn't just take up 220 bytes since the code has to go through the V8 JS engine (or other engines) and we don't know how much it's allocating for different things. – Garrett Smith Jan 01 '18 at 21:24
  • 1
    @GarrettSmith I was talking about networking cost. Execution of $ or jQuery is almost identical. – Ahmet Can Güven Jan 02 '18 at 10:42
  • 1
    If peoply tried to have to write less of something and applied that rule universally, that's how you get unreadable code. But I guess javascript developers consider unreadable code a benefit because that provides job security. – Preza8 Nov 19 '20 at 23:59
6

Google is your friend: $ sign JQuery

Dollar Sign is just an alias for JQuery.

jQuery(document).ready(function(){});

OR

$(document).ready(function(){});
Kashif
  • 12,701
  • 16
  • 62
  • 98
  • 5
    FYI: http://www.google.com.pk/search?q=jQuery%20sign&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla%3aen-US%3aofficial&client=firefox-a seems to give the same search results, which implies that Google doesn't actually allow the $ as a search term – Max Nanasy Sep 26 '14 at 02:44
4

In javascript, $ (a single dollar character) is a valid variable name. Several frameworks, among which jQuery, have adopted it as a synonym of an object that contain the top-level convenience methods the framework provides.

Confusion
  • 14,331
  • 7
  • 43
  • 71
1

$ sign is used as an alias to Jquery. instead of using jquery.hide,jquery.show every where we can use the alias $ ($.hide) Since we are using this word lot of times. 'Jquery' will not be a convenient way so we are using the alias $. If we want to change it we can change it by noConflict method var Sample=$.noConflict()

Lijo
  • 4,738
  • 1
  • 41
  • 55