58

Possible Duplicates:
Why would a javascript variable start with a dollar sign?
JQuery : What is the difference between “var test” and “var $test”

What is the difference between this two ways of initializing variables?

var $val = 'something'

     OR

var val = 'something'

as I see they are the same thing.

Maybe in this case $ is only the part of name in variable? (it will become a meaningless question in that case:/)

Thanks

Konerak
  • 39,272
  • 12
  • 98
  • 118
Simon
  • 22,637
  • 36
  • 92
  • 121

5 Answers5

106

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
  • 39,272
  • 12
  • 98
  • 118
  • 11
    Can you point to a reference that states that this is a jQuery convention? – Richard Ev Jul 29 '10 at 09:11
  • 3
    Actually the convention is use it only in software generated code. jQuery and other libraries are misusing it. – RoToRa Jul 29 '10 at 09:13
  • 5
    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 Jul 29 '10 at 09:17
  • 1
    Thanks man. i'm using jQuery for a long time, but i've learned new important details from your answer. Thanks;) – Simon Jul 29 '10 at 09:19
  • 1
    A convention that I was not familiar with... +1 – Ben Everard Jul 29 '10 at 09:21
  • I think that even hungarians do not use that anymore :-). I tend to rely on searching rather than programmer consistency. – mico Jul 29 '10 at 10:11
  • i think it isnt realy a convention but i think it would be nice if they make one of it – Spidfire Jul 29 '10 at 11:15
  • I've always used an array index to get from the jQuery variable to the DOM-variable: var myHeaderDiv = $myHeaderDiv[0]. Is there a better reason to use .get(0)? – Ben Sewards Nov 21 '14 at 16:22
12

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

the_drow
  • 18,571
  • 25
  • 126
  • 193
5

There are 28 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
  • 16,957
  • 5
  • 41
  • 48
  • 27? `The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.` As far as I can count - that's 28. Even your forum thread mentions that later on: `Oops, forgot that one - JavaScript has a 28 letter alphabet.` –  Oct 02 '17 at 23:38
5

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..

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
3

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 tollan
  • 22,305
  • 4
  • 49
  • 63