Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPratik Rai
6,856 Pointswhy are we putting $ sign in the value of a variable?
var $overlay = $('<div id="overlay"></div>'); var $image = $('<img>'); var $caption = $('<p></p>');
could anybody please explain, why are we putting $ sign in the variable as well in the values. is it necessary?
thank you
2 Answers
Chris Shaw
26,676 PointsHi Pratik,
The dollar sign is an unofficial way of defining variables that contain jQuery objects, it's not required and doesn't change the way variables behave but allows developers who aren't familiar with your code to understand it.
Hope that helps.
Jason Anders
Treehouse Moderator 145,860 PointsThe main thing to remember is that the $ is just shorthand and replaces the word 'jQuery.' So...
$("some code here");
//is the same as
jquery("some code here");
Pratik Rai
6,856 PointsPratik Rai
6,856 Pointsi understand in the variable but I meant about the values.
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsSorry, misread that. The
$
is required in the value as it denotes a new jQuery selector instance, if we remove it our code will either not run or complain about a syntax error.