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

Prefixing JavaScript variables with "$"

When creating variables, why does Andrew create his first variable (var color) without a dollar sign, but he creates his second variable (var $newColor) with a dollar sign? What is the difference? Does "var foo" denote one thing while "var $bar" denotes something else?

1 Answer

This is primarly a convetion and has no actual effect on the veriable. It is normally used to denote that the variable is a jquery object.

So is

var color = $(".selected").css("background-color");

a DOM object, whereas

var $newColor = $("<li></li>");

a jQuery object?

I have difficulty distinguishing between the two.

Both of those would still be jQuery objects as they are using the jquary selectors and functions. You could use the variable $newColor in a script without any jquery and it would have no effect.

jQuery objects are just wrappers for DOM elements.