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 2

On the $overlay

I couldn't quite understand why he had to add a dollar symbol to the front of the "overlay" variable? I understand its convention and that other characters would work, but why?

1 Answer

Brian Hernandez
Brian Hernandez
20,285 Points

So, at about 3:00 into the video, Andrew explains that by just leaving the code as:

var overlay = '<div id="overlay"></div>';

overlay is just set to a string but the objective is to make it a jQuery representation of this object we are creating so we can use it and add it and remove it around in our code. To do this he uses jQuery to select that div we just created:

var overlay = $('<div id="overlay"></div>');

and since now overlay is not just a string any more but a jQuery representation of this object, it is convention to kind of give it a little special flag or heads up that this overlay is not just some primitive data type like a number or string or a boolean but a jQuery representation. And Andrew is saying you can literally use anything for this type of flag... just that the dollar sign ($) is maybe the most widely used / recognized because in jQuery you use it so much to select other things so it is easy to tell that this overlay variable is special because it's a special jQuery selected "thing" and not just any old value. Literally it is a disembodied DOM object waiting to be thrown in somewhere and not just a string anymore.

I think this is the point of this part of the video to know though, that we don't just want a string but a DOM object that we can manipulate later in our code, and the $ (or whatever you chose to put in front of overlay) reminds us of this.