What is the difference between "$variable" and "variable" - JavaScript - jQuery

javascript, jquery

Solution

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.

Problem

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.

Original source

Related problems