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 Mobile Drop Down Menu Perform: Part 1

Calum Hartley
Calum Hartley
6,959 Points

Why do you need dollar sign before appending html to a page?

What is the difference between:

var $select = $("<select></select>"); $("#menu").append($select);

and

var $select = "<select></select>"; $("#menu").append($select);

Notice that the second version is without the $ sign and parentheses when assigning to the variable. I tested both and they both have the same outcome.

Thanks.

1 Answer

rydavim
rydavim
18,814 Points

For the purposes of the example you're working on, it probably doesn't matter.

However, they are not the same thing. $ is shorthand for jQuery, so that first version is the a jQuery object - whereas the second is just an empty string.

console.log($("")); // Object {  }
console.log(""); // (empty string)