Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$ at the beginning and at the end of variable [duplicate]

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?

like image 831
DarthVader Avatar asked Dec 04 '12 07:12

DarthVader


3 Answers

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$
like image 117
Guffa Avatar answered Oct 23 '22 01:10

Guffa


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 :)

like image 4
Nanne Avatar answered Oct 23 '22 01:10

Nanne


here

var hello$ and $foo

hello$ , $foo are just a variable name

and $("").blah();

$ is jquery object

jsFiddle

like image 2
NullPoiиteя Avatar answered Oct 22 '22 23:10

NullPoiиteя