55

I know it's a silly question but I am a bit confused with this. For example, if I have an input with an ID: rad1, is there any difference between below lines of code?

var $a = $('#rad1')

or

var a = $('#rad1')
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Exception
  • 8,111
  • 22
  • 85
  • 136
  • 2
    possible duplicate of [Why would a JavaScript variable start with a dollar sign?](http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign) – Nikolay Kostov Jul 27 '15 at 11:46

6 Answers6

79

No there is no real difference.

It's just a convention that helps you remember that a isn't the DOM element but it's a jQuery object.

var a = document.getElementById('a');
a.innerHTML  //fine

var $a = $('#a');
$a.html()   // fine

Ohhh, and by the way, neither a or $a are good variable names ... you should use meaningful variable names not abc characters.


Read the jQuery info tag on this very same site:

Variable Naming Conventions

jQuery wrapped variables are usually named starting with '$' to distinguish them from standard JavaScript objects.

var $this = $(this);
Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • 3
    I come from java background.. and this answer really enlightened my day.I just know that in JS I can use $ in var name.. – swdev Oct 02 '13 at 04:23
  • 6
    Funny thing is, you can [use $ in Java variables as well](http://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name). – zb226 Aug 04 '15 at 14:31
5

It's only for showing that it's a Jquery variable.

Declaring $a you're showing that your variable is for JQuery objects, it's just a notation. So the most readable thing will be to declare Jquery variable with $ notation

var $obj=$("#obj");

And DOM element without $ notation

var obj = document.getElementById("obj");
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
1

There's no difference. It's just a coding convention to help identify that the variable represents a jquery wrapped object.

kinakuta
  • 9,029
  • 1
  • 39
  • 48
1

No difference its just coding convention , check this

gdoron
  • 147,333
  • 58
  • 291
  • 367
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
1

I think this scenario should illustrate the reason for remembering(off course, assigning '$' ) jquery and simple javascript variables:

<form id='myform' >
  <input id="name" type="text" value="Peter" />
</form>

 <script>

 $(document).ready(function(){
      var name = document.getElementById('name');
  var $name = $('#name');

  console.log(name.value);   // javascript's properties are available 
       console.log($name.value);  //it is undefined  (worth of notice)

  console.log(name.val());   // error!  name.val is not a function !  (jquery function will not be available)
  console.log($name.val());    // jquery functions are available

});

 </script>
Shiv
  • 11
  • 1
0

A composition also works fine:

You can also do something like this to show a <div>:

function getCompTable(divId){
  var $d = $('#' + divId);
  $d.show();
}

USAGE

getCompTable('compListDiv'); // compListDiv - is a div id=""

Yosi Lev

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
ylev
  • 2,313
  • 1
  • 23
  • 16