The dollar sign is treated just like a normal letter or underscore ( _ ). It has no special significance to the interpreter. Unlike many similar languages, identifiers (such as functional and variable names) in Javascript can contain not only letters, numbers and underscores, but can also contain dollar signs.
Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.
The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code. The dollar sign ( $ ) and the underscore ( _ ) are permitted anywhere in an IdentifierName. As such, the $ sign may now be used freely in variable names.
In programming In languages like BASIC, Pascal, and PHP, the dollar sign defines variables and constants. ALGOL 68 and TeX typesetting languages use the dollar sign for delimiting transput format and mathematical regions. ASP.NET uses the dollar sign to indicate an expression.
The $
in the variable name is only part of the name, but the convention is to use it to start variable names when the variable represents a jQuery object.
var $myHeaderDiv = $('#header');
var myHeaderDiv = document.getElementById('header');
Now later in your code, you know the $myHeaderDiv
is already a jQuery object, so you can call jQuery functions:
$myHeaderDiv.fade();
var $myHeaderDiv = jQuery(myHeaderDiv); //assign to another variable
jQuery(myHeaderDiv).fade(); //use directly
//or, as the $ is aliased to the jQuery object if you don't specify otherwise:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign
$(myHeaderDiv).fade(); //use
var myHeaderDiv = $myHeaderDiv.get(0);
You are correct. $ is a part of the name of the variable.
This is not perl or PHP :)
No real difference..
It is usally used to signify a variable holding a jquery or other javascript framework object, because they can have shorthand $
function..
It is just easier to identify the type of the contents..
There are 28 letters in the alphabet as far as JavaScript is concerned. a-z, _ and $. Anywhere you can use a letter in JavaScript you can use $ as that letter. (<c> Fellgall @ http://www.webdeveloper.com/forum/showthread.php?t=186546)
In your example $val and val will be two different variable names.
syom - in my case, i use the $ prefix to indicate that it's a variable that is referenced by jquery. It's purely a part of the variable and not a reserved character.
keeps it easy to identify in long code runs..
jim
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With