views:

10824

answers:

6

Duplicate: http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign


Ok, bit of a noobie question for you,

Got some help with a previous question and I encountered something new to me.

The code in question is here:

var $item = $(this).parent().parent().find('input');

What is the purpose of the dollar sign in the variable name, why not just exclude it?

A pretty easy question for someone I'm sure, but when I don't understand something, it drives me nuts.

cheers, keith

A: 

No reason. Maybe the person who coded it came from PHP. It has the same effect as if you had named it "_item" or "item" or "item$$".

Maybe it's some kind of Hungarian notation for the coder to note that the variable is a DOM element or something.

Claudiu
A: 

There is no technical significance. They probably did it so as not to overwrite any existing 'item' variable, since it's a pretty generic name. Or possibly the coder has some kind of naming convention where, for example, any variables starting with a $ are DOM elements.

musicfreak
+3  A: 

The $ sign is an identifier for variables and functions since there are no types in Javascript.

http://www.authenticsociety.com/blog/JavaScript_DollarSign

That has a clear explanation of what the dollar sign is for.

AlbertoPL
+1 for the link.
Vadim
+7  A: 

A '$' in a variable means nothing to the interpreter, much like an underscore.

From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.

The dollar sign function $() in jQuery is a library function that is frequently used, so a short name is desirable.

cobbal
Note: By default, jQuery uses "$" as a shortcut for "jQuery". This has some effects on the use of other Javascript libraries. See http://docs.jquery.com/Using_jQuery_with_Other_Libraries
Thimmayya
+1  A: 

Unlike many similar languages, in Javascript an identifier (such as a variable name) may contain a dollar sign or even start with a dollar sign.

In some languages it may be convention to use the underscore (_) for special purposes at the start of an identifier, for example I've seen people use the underscore to indicate local variables.

In Javascript, either a dollar sign or underscore may be used for special purposes. Generally, an identifier beginning with a dollar sign is intended for "generated" code - this was suggested in a spec - but what you use it for is generally your own business, though it's generally not used in normal circumstances.

In your example, it looks like whoever chose the name '$item' for their variable may by a PHP programmer who did it out of habit, or something. In PHP, all variable names must start with a dollar sign.

Interestingly, having an identifier that is just a dollar sign, and nothing else, is something that jQuery does for its topmost-level object. It would be a good idea, if jQuery was the only thing in the world to use that identifier name. But it isn't, and therefore it isn't (a good idea), which you'll discover when you try to combine jQuery and Prototype, or any of the other libraries doing the same.

It would be much better for a Javascript library to use a distinctive, uppercase word as an identifier like YAHOO (as used in YUI).

That said, jQuery is a good library and it is not too difficult to make it use some other name than '$' if you need it to share with another library.

thomasrutter