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 trial

JavaScript jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 4

Why 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
Chris Malcolm
2,909 Points

the attribute "href" of the jquery object would return a string, not a jquery object so technically his convention is correct.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

Spot on Chris Malcolm!

Ryan Carson - with the $overlay you can call all the jQuery methods on it like hide() and show(). With the imageLocation it's just a plain JavaScript string and you can only call String methods on it, like toUpperCase(), not jQuery methods. Having the $ at the beginning just helps you remember which variables are jQuery objects.

Gracias! :)

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
Andrew Chalkley
Treehouse Guest Teacher

Good question...I'll bring it up with the community team.

Other 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/