75

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?

2
  • 2
    I actually didn't find anything useful. Missed that one! Thx. Commented Jun 2, 2011 at 1: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. Commented Jun 2, 2011 at 1:53

5 Answers 5

97

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);
Sign up to request clarification or add additional context in comments.

12 Comments

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.
In the context of using jQuery this is not outdated, but a regular practice.
Regular doesn't exclude outdated: An excellent read on Hungarian notation and jQuery: bennadel.com/blog/…
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 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.
@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".
|
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.

1 Comment

This seems like a good practice.
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() {/*...*/});

Comments

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.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.