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 trialRyan Carson
23,287 PointsWhy not add a '$' to the beginning of this variable?
Andrew said it's good convention to add something to the beginning of a variable if it's a jQuery object, like this:
var $overlay = $('<div id="overlay"></div>');
So why doesn't he add the '$' to the beginning of the imageLocation var?
var imageLocation = $(this).attr("href");
like this ...
var $imageLocation = $(this).attr("href");
2 Answers
Chris Malcolm
2,909 Pointsthe attribute "href" of the jquery object would return a string, not a jquery object so technically his convention is correct.
James Gill
Courses Plus Student 34,936 PointsOther than as a convention for clarity, it shouldn't matter. From the jQuery documentation:
"Many developers prefix a $ to the name of variables that contain jQuery objects in order to help differentiate. There is nothing magic about this practice – it just helps some people keep track of what different variables contain." http://learn.jquery.com/using-jquery-core/jquery-object/
Andrew Chalkley
Treehouse Guest TeacherAndrew Chalkley
Treehouse Guest TeacherSpot on Chris Malcolm!
Ryan Carson - with the
$overlay
you can call all the jQuery methods on it likehide()
andshow()
. With theimageLocation
it's just a plain JavaScript string and you can only call String methods on it, liketoUpperCase()
, not jQuery methods. Having the$
at the beginning just helps you remember which variables are jQuery objects.Ryan Carson
23,287 PointsRyan Carson
23,287 PointsGracias! :)
Andrew Chalkley - side note - why are questions associated to jQuery Basics automatically tagged with HTML instead of JavaScript? I had to change this one manually.
Andrew Chalkley
Treehouse Guest TeacherAndrew Chalkley
Treehouse Guest TeacherGood question...I'll bring it up with the community team.