It may be a silly question but I haven’t been able to find any documentations on this on the internet.
When declaring variables to use within Javascript i normally use var x = 0 but I’ve seen in jQuery tutorials that they use $x = 0. What is the difference of those two ?
Also, would i call those two variables the same way or do i need to use the $ mark before ?
So for example : for(i=0; i < x; i++) or for(i=0; i < $x; i++)
Your
var x = 0;is fine. The only reason people sometimes use a$with jQuery-specific variables is that it helps them remember that the variable contains the result of a call to the$()function in jQuery. The$is actually part of the variable name, so you would use$everywhere you referred to the variable.So this:
Is exactly the same as this:
They’ve just put a
$at the beginning of the name.Note: You’ve quoted your “jQuery” example as simply
$x = 0;(withoutvar). Be sure you always declare your variables (usingvar); otherwise, you’re falling prey to the Horror of Implicit Globals.