0

I'm working with a developer that never uses var when declaring variables in jQuery. Something like:

$x = "hello";
$y = "world";
$a = x + " " + y;

jQuery doesn't have a problem with this and he's been doing this for awhile. I've always used var:

var x = "hello";
var y = "world";
var a = x + " " + y;

We have run into one issue where the dollar sign caused an issue (it was a year ago so I can't recall the issue).

Does anyone know or have info on the difference between the two and why one is better or should be used over the other?

I've done some Google searching but they all talk about var $x; vs var x;

dcp3450
  • 10,959
  • 23
  • 58
  • 110
  • 2
    There isn't any difference except variable name between `var $x;` and `var x;`. `$` is usually used by convention when you want to specify that variable is a jQuery object – A. Wolff Mar 11 '14 at 14:49
  • does he declare the variable first like `var $x, $y, $a;`? and having $ is perfectly valid for a function or variable name. – Yusaf Khaliq Mar 11 '14 at 14:49
  • he never uses `var $x` just `$x` – dcp3450 Mar 11 '14 at 14:52
  • okay if he uses it within a function it will change the global variable with the name `$x` otherwise it's okay to use just `$x` which is will be declared globally. – Yusaf Khaliq Mar 11 '14 at 14:56

4 Answers4

4

Using a dollar sign isn't a replacement for var. $ in a variable name is the same as any other character. $ is often prepended to variables that contain a jQuery object. I.E:

var el = document.getElementById('id');

Versus:

var $el = $('#id');

So it's used primarily to differentiate. It makes no difference to the execution of your script.

var and $ in a reference are completely unrelated. In fact, in the global scope:

var $el = $('#id');

is the same as

$el = $('#id')
George
  • 36,413
  • 9
  • 66
  • 103
  • I've seen it used as `var $x = ..` before but he uses it without the `var`. – dcp3450 Mar 11 '14 at 14:54
  • 2
    [The rules of `var`](http://stackoverflow.com/a/2485439/1612146) apply whether or not the variable name contains a `$` or not. The two are unrelated. – George Mar 11 '14 at 14:55
0

The dollar sign is not doing anything special, it is just part of the variable name.

Declaring variables without var makes a global variable.

Community
  • 1
  • 1
Alex Wittig
  • 2,800
  • 1
  • 33
  • 42
0
$x = "hello";
$y = "world";
$a = x + " " + y;

This code is wrong. x and y is undefined.

Actually $x is not declaring x. $x is the same with var $x not with var x

$x is a variable name such any other one

You could write:

    x = "hello";
    y = "world";
    a = x + " " + y;

And it would be the same thing as:

    $x = "hello";
    $y = "world";
    $a = $x + " " + $y;
laaposto
  • 11,835
  • 15
  • 54
  • 71
0

having no var for variable defined globally will generally not cause an issue, when using a variable with the same name but different function/string you should declare it with var in another scope as to prevent it over riding the original global one.

http://jsfiddle.net/YWK7T/1/

$x = "hi#1";
alert($x); //alerts hi#1 as $x is declared globally
function hi(){
    var $x = "hi#2";
    alert($x); 
}
hi(); //alerts hi#2 as $x within the function scope is hi#2
alert($x); //alerts hi#1 the previous $x was defined within the scope.
function hi3(){
    $x = "hi#3";
}
hi3(); //replaces global vairiable $x to hi#3
alert($x); //alerts hi#3
Yusaf Khaliq
  • 3,333
  • 11
  • 42
  • 82