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

difference between var variable = "variable"; and var $variable = "variable";

I don't understood the difference between var variable = "variable"; and var $variable = "variable"; in jquery

I got just that it is a convenction used to recognize objects but I'm a bit confused about it

I also confuse var variable = "<p>hello</p>"; with var variable = $("<p>hello</p>");

why put the $() before the value of a variable to put inside it?

3 Answers

The difference is you have two variables. One called variable and one called $variable, and in your example they equal the same thing.

They are two different variable names.

for more info please see this link:

stackoverflow

"I also confuse var variable = "hello"; with var variable = $("hello");"

the difference between theses two is:

var variable = "hello" equals the string Hello

var variable = $("hello") equals the reference to a Jquery object called hello.

yes ok, I got the two differences but I cannot peek the real difference of the last why a string is not the same thing of a jquery object with a string inside?? there is a difference in the behavior??

ps the post wrote wrong, I wrote $(""); with inside a paragraph with inside "hello"

"In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:"

"Creating New Elements

If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it starts with <tag ... >). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements. You can perform any of the usual jQuery methods on this object"

this is taken from the Jquery documentation. I hope this clears up some confusion.

thanks