chromedude
chromedude

Reputation: 4302

What is the official correct way to declare jQuery variables?

I am trying to learn jQuery. I keep seeing people using different methods of declaring variables.

I have seen var variable;, var $variable;, and $variable;. Which one (if any of these) is the official correct way to declare jQuery variables.

Upvotes: 10

Views: 17347

Answers (6)

tvanfosson
tvanfosson

Reputation: 532595

Use var to define variables to ensure that they are scoped properly, usually locally within a function. My convention is to use $variable when the variable holds a jQuery object (result of a selector) and variable for everything else.

BTW -- they aren't jquery variables, but rather javascript variables. JQuery is just another (albeit the most popular at present) javascript framework, not a language in and of itself.

Upvotes: 4

Guffa
Guffa

Reputation: 700562

There is no such thing as a "jQuery variable", they are Javascript variables. When learning jQuery you have to learn some Javascript, as jQuery is just a library for Javascript.

You declare a variable using the var keyword. You can also use a variable without declaring it, and it will implicitly be declared as a global variable, but that is not recommended. You should declare variables in the scope where they are used.

Sometimes putting a $ sign as the first character of a variable name is used to indicate that it's intended for containing a reference to a jQuery object, however there is no official recommendation, and the language itself doesn't care what you name your variables.

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816780

Nick already explained why you should use var.

The reason behind variable and $variable is that $ is associated with jQuery and should imply that this variable holds a jQuery object. To avoid unnecessary function calls to jQuery like $(variable) where variable actually already holds a jQuery object.

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196187

There is no official correct way to declare variables for jQuery..

They are all javascript variables, and these are the rules for javascript variables

Using the $ at the beginning is a convention that the variable is holding a jQuery object (since the jquery uses the $ sign for the function wrapper)

The diffeernce of using var or not, is in the scope of the variable. Using var declares it local, not using var adds it to the global scope.

Upvotes: 3

Marek Sapota
Marek Sapota

Reputation: 20598

The ones with var are local variables, the one without it is global. So it's not just a matter of style. Apart from that use anything you like, personally I haven't seen "$" prefixed variables, but maybe they are useful if you mix a lot of jQuery and standard JavaScript code.

Upvotes: -2

Nick Craver
Nick Craver

Reputation: 630559

You should always declare a variable with var (whatever the scope, unless creating it on an object), implied globals are bad, don't do it.

As for variable vs. $variable, there is no official stance, just have a convention that you and your team stick to.

As a general rule some follow, (I don't follow this, some do), use variable for variables that aren't jQuery objects and $variable for variables that are, those who use this typically find that seeing $something and knowing immediately it's a jQuery object to be handy.

Upvotes: 23

Related Questions