Possible Duplicate:
Why would a JavaScript variable start with a dollar sign?
I realized some syntax with jquery and didnt know what it is. here goes:
var $foo = $("bar").blah();
var hello$ = $("stgelse").etc();
What is the difference between these? and why are they like that?
Also, For example:
var $foo = $("").blah();
var should already contain whatever right side is returning ? no?
Can you please elaborate?
In Javascript you can use $
in a variable name (or as a variable name all by itself, like it's used by the jQuery library for example), there is no special meaning in the language itself.
Some people use $
at the beginning of a variable name to indicate that the variable should contain a jQuery object, for example:
var $form = $('form');
A $
at the end of variable names was used in BASIC for string variables, which might be one reason for using it to mean something when naming variables in other languages also. Example:
10 LET NAME$="JOHN DOE"
20 PRINT NAME$
var $foo = $("bar").blah();
var
is a keyword that makes the variable local in certain cases. See this question
$foo
is the name of this variable. There is no need for the $
, and indeed it confuses everything, but some languages (like PHP) need them to say "this is a variable". It could've been just foo
=
is the assignment to make $foo
have whatever comes from the right hand side$
is another variable, but a special one: this is your JQuery thingy! From there you see it does all the JQuery stuff, like "find bar
, do blah()
etc.The same basically goes for the other line, but in this case someone thought that hello$
would be a nice name for a var :)
here
var hello$ and $foo
hello$
, $foo are just a variable name
and $("").blah();
$
is jquery object
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