74

Possible Duplicate:
Why would a javascript variable start with a dollar sign?

I see people using the dollar sign in front of variables when using jQuery. Is there any reason behind this? I'm I missing something basic or is it just a common practice?

Community
  • 1
  • 1
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 2
    I actually didn't find anything useful. Missed that one! Thx. – elclanrs Jun 02 '11 at 01:47
  • 5
    This isn't a duplicate for that question -- this question *focuses explicitly on usage in a jQuery context* and has received a completely different set of responses. –  Jun 02 '11 at 01:53

5 Answers5

96

It's a common reference to a jQuery wrapped object. It makes reading the code easier to know which variables are jQuery wrapped.

//Item has been "cached" for later use in the script as a jQuery object.
var $item = $(this);
Scott Harwell
  • 7,457
  • 2
  • 28
  • 41
  • 11
    Using the $ in front of variable names is what is called "hungarian notation" and is no different from doing int_count, arry_list or str_name. This is not wrong, but many people see this as an outdated way of naming variables. – Bert Goethals Jun 02 '11 at 01:33
  • 20
    In the context of using jQuery this is not outdated, but a regular practice. – Scott Harwell Jun 02 '11 at 01:35
  • 13
    Regular doesn't exclude outdated: An excellent read on Hungarian notation and jQuery: http://www.bennadel.com/blog/1778-Using-Variable-In-jQuery-Code-Is-Just-Hungarian-Notation.htm – Bert Goethals Jun 02 '11 at 01:45
  • 2
    An opinion -- not fact -- based blog post with a conclusion that basically states that it's a preference to use camel case over hungarian notation. A hybrid of both achieves maximum readability. – Scott Harwell Jun 02 '11 at 01:48
  • 5
    I have never used -- or seen -- `$item` notation used in code I have worked on, even to indicate it contains a jQuery object. However, this usage *might* be [arguably Apps Hungarian](http://en.wikipedia.org/wiki/Hungarian_notation#Systems_vs._Apps_Hungarian) as the variable "represent a jQuery object" and not System Hungarian (which is normally considered "the bad kind"). The line is very blurred due to lack of static type information, of course. –  Jun 02 '11 at 01:50
  • 2
    The fact that the guy had to go on stackoverflow to figure out what the $ prefix meant is an indicator that it is a bad practice. Code should be self-describing – Breakskater Jun 26 '13 at 17:48
  • 4
    @Breakskater , an assignment statement is about as self describing as any code could be. X = Y ... no other way to say that. If folks didn't have to come here for reference regarding how to do something or what something meant, then there would be no point to have Stack Overflow, reference books, etc.; it would all be "obvious". – Scott Harwell Nov 15 '13 at 14:59
  • 1
    I've seen this notation used in typescript too and don't see the reason for it, since we have intellisense and it's just an extra letter which makes it look ugly. Code should be beautiful and readable without having to prefix variable names. I'd be more concerned with using the variable name item instead of prefixing variables with $. If someone is looking over some code that is making use of JQuery, it should be clear enough that they are going to come across JQuery objects. Just my opinion :) – Patrick Magee Feb 21 '14 at 14:18
  • 1
    I like the `$` prefix convention and will continue using it. :) – Ilia Choly Sep 13 '14 at 16:24
  • personally this notation is an annoyance to the readers eye. currently I am reading a "code" that littered with $ prefix. – Tony Shih Oct 18 '14 at 16:57
  • 1
    Hungarian notation is of little use when you have a type system — not in JavaScript. And jQuery code sometimes *mixes* jQuery-wrapper object and not-wrapped objects. – Blaisorblade May 06 '16 at 11:19
  • when you come back to jQuery after a long pause, you feel the need to have _used_ $ for cached jQuery objects... _learnt_ to use it now onwards – Irf Oct 18 '19 at 10:19
44

For me a common practice is this:

If a variable is private I use an underscore like this:

(function(){
     var _foo = "bar";
})()

If it's public I'll use no underscore:

var foo = "bar"

And if it's a jQuery selector I'll use the $:

var $foo = $('bar');
//then you can access it like this
$foo.attr('id')

It's just coding convention and it allows you to quickly reference what type the variable is later in the code.

morten.c
  • 3,414
  • 5
  • 40
  • 45
locrizak
  • 12,192
  • 12
  • 60
  • 80
7

Many people using jQuery will prefix variables that contain a jQuery object with a $, so that they are easily identified. Consider this example:

var $img = $(".someclass span.otherclass img");
/* somewhere later in the code */
$img.bind("click", function() {/*...*/});
sanmai
  • 29,083
  • 12
  • 64
  • 76
3

In my experience this is just a readability. Some developers like to prefix their variables so that they are easy to spot. It could also be a PHP habit creeping it's way in to Javascript.

supajb
  • 487
  • 6
  • 11
2

Dollar signs in code that uses JQuery commonly means that the variable in question is a jQuery variable (an object wrapped by jquery).

Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
Marino Šimić
  • 7,318
  • 1
  • 31
  • 61