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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
variablevs.$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
variablefor variables that aren’t jQuery objects and$variablefor variables that are, those who use this typically find that seeing$somethingand knowing immediately it’s a jQuery object to be handy.