0

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

JavaSript

var img = $('#redImage');

jQuery

var $img = $('#redImage');
O P
  • 2,327
  • 10
  • 40
  • 73
  • possible duplicate of [The $ dollar sign](http://stackoverflow.com/questions/1180213/the-dollar-sign) – Evan Trimboli May 07 '13 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). – Quentin May 07 '13 at 22:25

3 Answers3

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
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

Community
  • 1
  • 1
EmeraldD.
  • 150
  • 6