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

When to use $ sign for a variable name?

In his jQuery Basics, Andrew sometimes uses a $ sign to start a variable name. For example:

var $canvas = $("canvas");

What is the reason for that? Does anybody know?

3 Answers

It's basically to differentiate between a something that is wrapped in jQuery and something that isn't. For example, something that is wrapped in jquery you could then call another jQuery method on whereas a normal javascript variable can be used to refer to just an element that isn't wrapped in jQuery and so you wouldn't be able to call a jQuery method on it

He explained it when he first used it :) He uses that prefix to show that it's a jQuery representation of an object.

Thanks for the replies. That really helped. It was just confusing since Andrew didn't do it on, color and context:

var color = $(".selected").css("background-color"); var $canvas = $("canvas"); var context = $canvas[0].getContext("2d"); var lastEvent;