0

What is the difference between these two methods of establishing a variable?

JavaSript

var img = $('#redImage');

jQuery

var $img = $('#redImage');
2
  • possible duplicate of The $ dollar sign Commented May 7, 2013 at 22:21
  • Your headings are a bit odd. Both those examples are JavaScript and both depend on the jQuery library (or another function named $ that expects the first argument to be a string containing a CSS selector). Commented May 7, 2013 at 22:25

3 Answers 3

8

The have different names (one of them starts with a $ sign, the other does not). That is all. The $ has no special meaning, it is just a character.

Some people use variables starting with a $ as a hint that the variable is expected to have a jQuery object assigned to it. This is much like the Hungarian Notation style of prefixing variable names with letters to indicate the type.

Sign up to request clarification or add additional context in comments.

Comments

2

$ in var $img is just a naming convention to signify that the variable is a jQuery object

While Doing this -

var img = $('#redImage');
var $img = $('#redImage');

You actually defined two variables with different name - img and $img

Comments

1

One creates a variable named img and the other creates a variable name $img ... The dollar sign is a perfectly valid character in a variable name.

Valid Characters in Javascript Variable Names

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.