6

The $ dollar sign in front of (this) serves to select the current element. I'm a little confused about the $ in front of option and thisSelect. Do they have a special meaning?

var $options = $(this).children('option');
var $thisSelect = $(this);

Thanks for helping

Richard77
  • 20,343
  • 46
  • 150
  • 252

6 Answers6

15

As everyone said, its just a convention.
I use the $ sign in front of a variable to identify that this variable holds an object.

var thisIsANumber = 1024; // No $.. Its a normal variable
var $divElement = $('div#idOfDiv'); // Yes! Its a jQuery Object
var $this = $(this); // Commonly used to reduce the work javascript has to do!

//Now I can use something like this.. (Notice how easy it is to read!)
$divElement.slideUp();

// Or a more `real world` example!
$('#element').click(function(){
    // Hold $(this) inside a variable
    // So we don't have to traverse the dom unnecessarily
    var $this = $(this); // Save it (its a object.. so prepend a `$` )
    $this.hide(); // Use it again
    $this.fadeIn(); // and again
//  ^ Has a dollar sign, because it is a jQuery Object.
});

You will see loads of plugins use this convention (well.. at least the well written ones).
By storing the object inside a variable, Javascript doesn't have to crawl through your code each time to get the element. Instead, we already have the element (inside the variable), so we use that to reference it.

If you use $(this) more then once inside the same callback function, you should store it inside a variable.. (var $this = $(this);). Otherwise, every time you use it, javascript will have to get the element from your source code each time (which can massively decrease performance! (especially for people browsing on a slow/old computer!).

Anil
  • 21,730
  • 9
  • 73
  • 100
9

It's a common reference to a jQuery wrapped object. It makes reading the code easier to know which variables are jQuery wrapped.

//Item has been "cached" for later use in the script as a jQuery object.
var $item = $(this);

Other common practices:

If a variable is private then use an underscore like this:

(function(){
     var _foo = "bar";
})()

If it's public we use no underscore:

var foo = "bar"

and if it's a jQuery selector we use the $:

var $foo = $('bar');    
//then you can access it like this
$foo.attr('id')

It's just a coding convention and it allows you to quickly reference what type the variable is later in the code.

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
2

the dollar sign can help indicate when your variable contains a jQuery object as opposed to any other type. it is purely up to the coder if they want to include the $ sign or not in front of the var, it just serves as a reminder.

Evan
  • 5,975
  • 8
  • 34
  • 63
2

There is no meaning. Those are simply normal characters, just like _ or π (this one not safe if you don't master your toolchain) that you can put in your variable names.

See here the specification regarding valid javascript name. And especially this :

This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

You might also be interested by this related answer.

It's usual to prefix with $ variables contain jQuery sets.

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

$ sign before these variable name is just like other characters in variable name. It does not have any significance. You can use this convention to identify that you have jQuery object in this varible.

Adil
  • 146,340
  • 25
  • 209
  • 204
1

Some people used to addopt the convention to add $ in front of a var name to know that it's value is a jQuery object.

This way I know that the following vars have different results.

var $this = $(this);
var self = this;
Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82