0

Is there any difference between defining variables:

var p_tr1 = $('<tr> </tr>')
$p_tr = $('<tr></tr>')

i'm using netbeans, and variables have different highlighting. I looked through FF debugger and found that variables are equals

meteor
  • 658
  • 1
  • 9
  • 19
  • 3
    the `$` makes no difference. It's just part of the name. The lack of `var` in the second case does make a difference however. With `var` you are creating a variable in the current scope. Without you are going to walk up the scope chain and set which ever variable you find with the same name, or you'll create a new global variable (if no matching variable name is found). – Matt Burland Jul 31 '14 at 20:04
  • 1
    Nothing! Generally `$` is used to indicate jQuery objects -- but you could use it whenever you want. – Casey Falk Jul 31 '14 at 20:04
  • 1
    @MattBurland, I tip my hat at your speed. – Casey Falk Jul 31 '14 at 20:05
  • 2
    @CaseyFalk: I've seen some people do that, but it doesn't seem to be a widespread thing. Most people don't use `$` in their javascript variables unless they are coming from php. – Matt Burland Jul 31 '14 at 20:05

3 Answers3

4

There's no difference whatsoever, historically it has been used to denote jQuery objects.

var $td = $('td') // common use case

If you use jQuery, it might be useful to know at any point if the variable you're working with is already wrapped or not.

Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
1

The $ sign is just a symbol that can be used in variable names. Creating a variable a = 1 is the exact same thing as creating a variable $a = 1, or another variable a$ap = 1. The only thing that changes is the name of the variable. You can type a == a$ap and it will output true.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
1

There is no difference, it's for code readability and easier understanding, because usually $prefix is used for a variable when you have a jQuery wrapped result. So in case you select an element with id mydiv:

var $mydiv = $('#mydiv');

But if you would have it's non jQuery wrapped counterpart, you would do:

var mydiv = $('#mydiv')[0];

This way you know that with the 1st one you can use jQuery functions and with second one you can't.

the berserker
  • 1,553
  • 3
  • 22
  • 39