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

CSS jQuery Basics (2014) Creating a Simple Lightbox Perform: Part 2

Pratik Rai
Pratik Rai
6,856 Points

why 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
Chris Shaw
26,676 Points

Hi 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.

Pratik Rai
Pratik Rai
6,856 Points

i understand in the variable but I meant about the values.

Chris Shaw
Chris Shaw
26,676 Points

Sorry, 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.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

The 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");