Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,008 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.