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

Tiny JQuery issue!

I'm working through the JQuery stages at the moment and got stuck on one of the code challenges. I looked around at other people's solutions and noticed that I had a $ at the beginning of a variable name where as they did not.

My question is this: why is a $ sometimes used before a variable name such as in the videos, showing code as follows:

var $submit = $(".required");...

but is sometimes typed without the $ as in the solution to the code challenge:

var values = $(".required").map(function(){...

I hope this makes sense, I feel like somewhere along the line I've missed something important :/

Thanks! Spen

2 Answers

Its always optional to put the $ at the start of a variable. In the tutorial it was recommended so you knew exactly where is was from. Like

$('#container').onClick(function(){ var $container = new (this) $container.show() }); (Something like that was the awful example)

I tend to ignore putting a $ infront of variables. The $ symbol is designed to show a selector in jQuery. If you start putting them infront of variables it becomes messy which is which.

Ahh brilliant! Thanks Jason! I hate being unsure of something before moving forward, so the clarity is a big help :)