1

From what i understand, JavaScript requires variable to be defined using var keyword, and jQuery allows to define variables using $ symbol, I am wondering what added benefits does it bring when i define variable using jQuery, or is it purely preference? why will i use one convention over the other?

Also, what is the difference between the following variable declarations?

  • var foo
  • var $oo
  • $foo
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
  • 2
    _“JavaScript requires variable to be defined using var keyword”_ – no, not necessarily. But without the `var` keyword they will always be global, not matter where that initialization occurs. And the $ part does not “define” a variable in itself, it is just part of the variable _name_. – CBroe Mar 19 '15 at 04:57
  • See http://stackoverflow.com/questions/1916584/jquery-variable-syntax – jkd Mar 19 '15 at 04:57
  • 2
    Don't use `$` in variable names. It's hideous. I know it's *somewhat* of a convention for jQuery users but it's a terrible one. There's just no need. You should have sensible enough names that you don't need a `$` to know it's a jQuery object. –  Mar 19 '15 at 05:02
  • > JavaScript requires variable to be defined using var keyword, and jQuery allows to define variables using $ symbol this is wrong. JQuery is a (big) JavaScript framework, not a JavaScript "addon", you can't define a variable in JQuery, you just access to JQuery using $() - not $name http://www.w3schools.com/jquery/jquery_syntax.asp – Simone Sanfratello Mar 19 '15 at 05:12

2 Answers2

5

It is common to use a $ in front of the variable name when the variable is a jQuery object. Say for instance var $body = jQuery('body'); is a way to let other developers reading the code(and your future self) know they are dealing with a jQuery object instead of the body element directly. Definitely a preference thing, I would say decide what works best for your team and stick with it!

Ryan
  • 5,845
  • 32
  • 27
1

The dollar sign is not a variable definition. It is short for jQuery. More exactly $ === jQuery. Edit: This is in general, not in variable definition (as CBroe specified in comment).

So the fact that some use it for variable declaration is purely preference, and in my opinion a misguiding one. Or maybe they have php background and are used to the $varName definition from there.

About the differences, you have difference between:

var foo

and

foo

The first one is a local definition, while the second is global.

Example:

  function test() {
      var a = 2;
      b = 5;
  }

  test();
  console.log(a); // undefined
  console.log(b); // 5
zozo
  • 8,230
  • 19
  • 79
  • 134
  • 1
    _“It is short for jQuery”_ – no, in `$foo` it is simply part of the variable name. (In `$(…)` it references the jQuery object, yes.) – CBroe Mar 19 '15 at 04:58
  • That's what I meant, read the following line. "So the fact that some use it for variable declaration is purely preference, and in my opinion a misguiding one". Maybe I should rephrase that? – zozo Mar 19 '15 at 04:59
  • 3
    Since OP used `$foo` as example, it might be misunderstood … but lets not nitpick, I just wanted to be clear about it. As for using it in variable names, which you consider “misguiding”, I think it makes sense when it is used to signify that a variable actually holds a jQuery object. `var $elements = $('.selector')` – CBroe Mar 19 '15 at 05:04