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 Drawing Application Perform: Part 1

Why in this case var is color and not $color?

Not 100% sure on this one... I rest of the projects here at the JQuery basics course every var created was named with the '$' before hand. In this case var is just color.

Essentially, what is the difference between the following? var color = $(".selected").css("background-color"); or var $color = $(".selected").css("background-color");

2 Answers

In another video Andrew explained that using the $ sign to start variable names is optional. He said he does it when what's being selected is an element. And other people have different preferences or personal rules. The example he gave was:

var $overlay = $('<div id="overlay"></div>');
Ran ShemTov
Ran ShemTov
14,148 Points

there is obviously no need to use $ before veriables in JQ. the dollar sign, however, is used as an important feature for something else: putting your variable in dollar sign and brackets - $(var) - will turn it into a JQ object. Why do we need to turn it into a jq object? here's an example: if you want to get the value of some variable of yours. you would use

var.value;

for simple JS.

if you turn it into a jq object, you can use .val(). example:

$(var).val();