Welcome to the Treehouse Community
Looking to learn something new?
How to determine if I should use $ before a variable name?
This may have been explained somewhere, but how do you determine if you should set $ before a variable name in JS? I have an example below.
var color = $(".selected").css("background-color"); var $canvas = $("canvas"); var context = $canvas.getContext("2d");
If you're following that convention, then you would put a $ in front of any variable name that is storing a jQuery object.
For the first line, that code is retrieving the value of the background color for the select element. This is going to come back as a simple string and not a jQuery object. So the
color variable would not have a $ in front because it's storing a string.
The second example is selecting the canvas element and wrapping it in a jQuery object. The canvas variable would get the $ in front since it is storing a jQuery object.
The 3rd example is accessing the canvas element itself with
$canvas and then calling the getContext() method on that. This returns a drawing context and not a jQuery object so the
context variable doesn't get the $ prefix.
Kyle Johnson33,527 Points
I'm pretty sure jQuery uses the
$ is equal to
james southFront End Web Development Techdegree Graduate 33,258 Points
the dollar sign is the jquery alias. if you are just using plain JS you probably wouldn't be putting it in front of your variables. in jquery it denotes a jquery object. it is a shorthand notation for jquery.