views:

372

answers:

5
+7  A: 

You are correct. $ is a part of the name of the variable.
This is not perl or PHP :)

the_drow
Or QBASIC! (ahh, nostalgia)
Konerak
+3  A: 

There are 27 letters in the alphabet as far as JavaScript is concerned. a-z and $. Anywhere you can use a letter in JavaScript you can use $ as that letter. (<c> Fellgall @ http://www.webdeveloper.com/forum/showthread.php?t=186546)

In your example $val and val will be two different variable names.

Māris Kiseļovs
+3  A: 

syom - in my case, i use the $ prefix to indicate that it's a variable that is referenced by jquery. It's purely a part of the variable and not a reserved character.

keeps it easy to identify in long code runs..

jim

jim
+4  A: 

No real difference..

It is usally used to signify a variable holding a jquery or other javascript framework object, because they can have shorthand $ function..

It is just easier to identify the type of the contents..

Gaby
+20  A: 

The $ in the variable name is only part of the name, but the convention is to use it to start variable names when the variable represents a jQuery object.

var $myHeaderDiv = $('#header');
var myHeaderDiv = document.getElementById('header');

Now later in your code, you know the $myHeaderDiv is already a jQuery object, so you can call jQuery functions:

$myHeaderDiv.fade();

To get from the DOM-variable to the jQuery variable:

var $myHeaderDiv = jQuery(myHeaderDiv); //assign to another variable
jQuery(myHeaderDiv).fade(); //use directly

//or, as the $ is aliased to the jQuery object if you don't specify otherwise:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign
$(myHeaderDiv).fade(); //use

To get from the jQuery variable to the DOM-variable.

var myHeaderDiv = $myHeaderDiv.get(0);
Konerak
Can you point to a reference that states that this is a jQuery convention?
Richard Ev
Actually the convention is use it only in software generated code. jQuery and other libraries are misusing it.
RoToRa
It's a variant of hungarian notation really - instead of t_name to remind the programmer the name is still tainted, it uses $ to remind the programmer this is already a jQuery object.
Konerak
Thanks man. i'm using jQuery for a long time, but i've learned new important details from your answer. Thanks;)
Syom
A convention that I was not familiar with... +1
ILMV
I think that even hungarians do not use that anymore :-). I tend to rely on searching rather than programmer consistency.
mico
i think it isnt realy a convention but i think it would be nice if they make one of it
Spidfire