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

carine todmia
carine todmia
6,071 Points

using the $ to label variables

Hi I am following the simple image gallery tutorial and in it the instructor labeled his variable starting with a $. when i did this my code did not work. when i took it out it worked. Can someone explain this discrepency. honestly from what i recall from javascript basics you cant do this anyways.

3 Answers

carine todmia
carine todmia
6,071 Points

hey guys thanks for the help looks like there was just a small error in the code that i wasnt catching.

Hi Carine,

Can you please post all of your JavaScript code? There should not be a problem with prefixing variables with a $ in JavaScript - I do it all the time, for the same reason Andrew mentions - it's convention to convey that it holds a jQuery object.

Just to elaborate on what Robert said with an example, I tend to find it useful to use $ on variables when it represents a DOM object. For example, a text input:

var $name = $('#name');

However, let's say we also wanted a variable that had the value of name:

var nameValue = $name.val();

or, assuming you didn't already have $name defined:

var nameValue = $('#email').val();

Where nameValue is referring to the actual value of the input, and not the input element itself.