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 3

Why does the variable have a jQuery $ in front of it? var $newColor = $("<li></li>");

I'm used to seeing variables without the JQ $, like

var input = ("blah blah").append();

But this variable has the JQ $ prefixed to it. What gives? How do we know when to go either way?

2 Answers

The $ in front of the variable, in this case, is just a practice that many people use when writing jQuery. It's to show that this is a jQuery object. Its up to you if you follow this practice, it doesn't affect the output of code in any way.

var $element = $('.element');
var element = $('.element');

Aside from the variable name both of these would produce the same result.

jason chan
jason chan
31,009 Points
// you can store variables like this it's called caching
var input = $(".selector").append();
input.removeClass("red");

$ is just a shortcut for jQuery. The idea is that everything is done with the one global symbol (since the global namespaces is ridiculously crowded), jQuery, but you can use $ (because it's shorter) if you like:

You will see when you get longer in your journey in programming. It has to do with scope.