views:

9763

answers:

8

I quite often see javascript with variables that start with a dollar sign. When/why would you choose to prefix a variable in this way?

(I'm not asking about $('p.foo') syntax that you see in jQuery and others, but normal variables like $name and $order)

+13  A: 

It's probably a habit picked up from Perl programming.

(edit: or PHP)

brien
PHP guys do that too.
Mnebuerquo
ditto on the PHP, you might just edit your response to say that brien.
Anthony Potts
PHP requires it.
eyelidlessness
QBASIC does it as well...
matt lohkamp
"QBASIC does it as well"... Too bad we can't upmod the comments ! ... :-D ...
paercebal
we can now!!!! ahahah
Claudiu
+3  A: 

Some languages require it, such as PHP or Perl - I'm guessing that the dev didn't remember that it's not needed in javascript.

Rich Bradshaw
+2  A: 

While you can simply use it to prefix your identifiers, it's supposed to be used for generated code, such as replacement tokens in a template, for example.

Rorschach
+17  A: 

AFAICS it's not recommended to use because the ECMAScript specification states that:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code.

cic
+21  A: 

As others have mentioned the dollar sign is intended to be used by mechanically generated code. However, that convention has been broken by some wildly popular JavaScript libraries. JQuery, Prototype and MS AJAX (AKA Atlas) all use this character in their identifiers (or as an entire identifier).

In short you can use the $ whenever you want. (The interpreter won't complain.) The question is when do you want to use it?

I personally do not use it, but I think its use is valid. I think MS AJAX uses it to signify that a function is an alias for some more verbose call.

For example:

var $get = function(id) { return document.getElementById(id); }

That seems like a reasonable convention.

Benry
+25  A: 

A very common use in jQuery is to distinguish jQuery objects stored in variables from other variables. For example, I would define

var $email = $("#email"); // refers to the jQuery object representation of the dom object
var email_field = $("#email").get(0); // refers to the dom object itself

I find this to be very helpful in writing jQuery code and makes it easy to see jQuery objects which have a different set of properties.

jonstjohn
+1 I too have adopted this and find it helpful!
alex
+1  A: 

The $ character has no special meaning to the JavaScript engine. It's just another valid character in a variable name like a-z, A-Z, _, 0-9, etc...

+2  A: 

Stevo is right, the meaning and usage of the dollar script sign (in Javascript and the jQuery platform, but not in PHP) is completely semantic. $ is a character that can be used as part of an identifier name. In addition, the dollar sign is perhaps not the most "weird" thing you can encounter in Javascript. Here are some examples of valid identifier names:

var _ = function() { alert("hello from _"); } var \u0024 = function() { alert("hello from $ defined as u0024"); } var Ø = function() { alert("hello from Ø"); } var $$$$$ = function() { alert("hello from $$$$$"); }

All of the examples above will work.

Try them.