8

I have initialized 2 variables, var1 and $var2 under $(document).ready(function() in jQuery. What is the main difference (or the possible differences) between these 2 variables?

var1 = "var1";
$var2 = "var2";
$('#click1').click(function() {
    alert(var1);
});
$('#click2').click(function() {
    alert($var2);
});

Here is the working fiddle.

Alfred
  • 21,058
  • 61
  • 167
  • 249
  • can you explain a little bit? which one is global? and what do that mean? – Alfred Mar 16 '13 at 03:23
  • They're the same thing. "$" is a legal character for a variable name, like "t" or "a". – Xyan Ewing Mar 16 '13 at 03:25
  • They're just spelled differently; no relation to jquery, you can start variable names with $ and have $MyVariableName. As your code is, they're just strings. – frenchie Mar 16 '13 at 03:25
  • 2
    A common use of `$` as a prefix for a variable name is to indicate this variable contains a jQuery object. As such using the `$` Hungarian notation for variables which do **not** hold jQuery objects will cause tons of confusion. As stated already, using `$` for none jQuery object variables is perfectly fine just be aware of the common understanding of variable names among your fellow developers working on the same project and how they might perceive the code. – Nope Mar 16 '13 at 03:41

3 Answers3

18

There is no difference. Javascript allows the $ character in identifiers, such as variable and function names, just as it allows letters, digits, and certain other punctuation characters to be used. It has no special meaning.

jQuery sets the global $ variable to an object with a number of special behaviors, so variables beginning with $ are often reserved for variables or values related to jQuery. This is not enforced at any level, though. You're free to use $ in variable names wherever and however you like.

  • 1
    +1 good answer. I personally like to avoid `$varname` in JavaScript because it helps to keep JavaScript syntax separate from PHP syntax ;) – Niet the Dark Absol Mar 16 '13 at 03:28
  • 2
    +1. I like to use `$` for variables that I know will hold jQuery objects - just an extra reminder to myself what they are. (And I don't do PHP so I don't have Kolink's worry.) – nnnnnn Mar 16 '13 at 03:30
  • And I sometimes use `var` in PHP. – Ram Mar 16 '13 at 03:32
1

Actually they are the same. The "$" sign is used to indicate that the variable is used with jQuery. This is the convenient way for developer to note it. You can use both "var1" and "$var2" in pure javascript and jquery.

You should consider the rules to declare variable in JavaScript, you will see that you can use the "$" sign in your variable.

HENG Vongkol
  • 883
  • 1
  • 8
  • 10
0

I don't think there are any difference between the two variables' scope. It's just that $var2 has a '$' sign in its variable name, and holds a different string value.

I found this thread to explain JavaScript scope quite well.

Community
  • 1
  • 1
user1766760
  • 631
  • 2
  • 8
  • 26