41

I have something simple like this:

$(selector).append("somestuff");

But since I'm going to reuse the selector I cache it with:

var $selector = $(selector);

So I end up with:

$selector.append("somestuff");

My question is, should I be doing that, or should I be doing:

var selector = $(selector);
selector.append("somestuff");

Trying either one, both works. Which method is correct, and why? Is the $ in $selector unnecessary because the jquery object has already been declared in $(selector)?


Edit

Thanks for the answers. It seems very simple and quite clear. Still, there seems to be disagreement over whether or not I should use $ in the variable. It would be nice for everyone to vote up an answer. :)

Community
  • 1
  • 1
Mark
  • 32,293
  • 33
  • 107
  • 137
  • Your question indicates you're a little confused about how objects behave. You might want to review a bit of object oriented programming basics. – Spencer Ruport Jul 24 '09 at 21:28

9 Answers9

48

$ is just a name - names in JavaScript can contain dollar signs, and can consist of just a dollar sign.

Whether you use a dollar sign in your name isn't relevant to jQuery - there's nothing special about the dollar sign, except that jQuery defines a function called $.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
33

I've seen it done both ways. All you are doing is creating a variable with the name '$selector', so they are functionally equivalent. The nice thing about it is that it does make them easy to pick out as jQuery objects.

Mara Morton
  • 4,429
  • 1
  • 21
  • 12
21

The cash sign is just an alias for the jQuery function. Starting the variable name with $ has no effect on that variable.

It is customary though, especially in jQuery plugin development, to use variables already wrapped in jQuery with a cash sign, so you know you can call jQuery methods, without having to wrap them.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ben Crouse
  • 8,290
  • 5
  • 35
  • 50
  • 7
    +1. $ prefix to your variable indicates that it's a jQuery object and you can call all the jQuery methods without thinking whether you need to pass the variable to the jQuery method. – SolutionYogi Jul 24 '09 at 21:28
11

RichieHindle is correct. To expand:

Javascript variables allow the '$' character. So for example, you could have the following:

var $i = 1;
var i = 1;

Both i and $i have the same value, and both are perfectly legitimate variable names.

jQuery assigns itself (the jQuery object) to '$' because traditionally, that's what Javascript frameworks have done for selectors. There's no inherent meaning to '$' beyond what jQuery gives it.

thedz
  • 5,496
  • 3
  • 25
  • 29
  • 1
    I think JavaScript frameworks chose `$` for this because most people were not really aware it could be used in variable names so it rarely clashed when introducing frameworks into existing code. – hippietrail Jan 06 '12 at 11:13
9

I like to prefix all my jQuery object names with $ so that I know it's actually a jQuery object, not a DOM reference.

It's a good naming convention.

Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
mkoryak
  • 57,086
  • 61
  • 201
  • 257
3

In this case, $selector is an example of Hungarian notation. It is there so that you know that $selector is a jQuery object.

If you make it your custom to always have jQuery objects start with $ if you suddenly use one wrong it will look wrong which will help you find a problem in your code.

The language couldn't care less. They could have just as well named the jQuery function J or anything else; the $ is not a special symbol.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Zachary K
  • 3,205
  • 1
  • 29
  • 36
2

Yeah.

jQuery always returns a jQuery object. It's what allows chaining.

You've already defined the variable so you get a jQuery object back so $ is unnecessary

easement
  • 6,119
  • 3
  • 29
  • 36
-1

I don't believe that you need to add the jQuery selector to jQuery objects (since it's already a part of the object). We don't add the selector and haven't run into any problems.

Grant Heaslip
  • 967
  • 2
  • 10
  • 16
-2

"$" is a function in jQuery. So when you call $(selector), you're actually calling the function $ with selector as the argument.

Generally, don't use the "$" as part of a variable name for javascript. You will only confuse yourself.

vh.
  • 147
  • 1
  • 1
  • 6
  • 1
    $ is not a function, just an alias. Also, using $ as part of a variable name is a good practice to identify that that variable is a jQuery variable. – ricardohdz May 14 '13 at 18:28