What is the difference between these two methods of establishing a variable?
JavaSript
var img = $('#redImage');
jQuery
var $img = $('#redImage');
What is the difference between these two methods of establishing a variable?
JavaSript
var img = $('#redImage');
jQuery
var $img = $('#redImage');
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.
$
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
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.