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 trialcyber voyager
8,859 PointsA simple question.
Hello and thank you for reading and answering this question, as I was reading the answer of this coding challenge I saw that Dave McFarland wrote this code:
var $searchField = $('#search');
But I wrote it like this:
var searchField = $('#search');
What is the difference between these two? Why Dave choose the first method?
Thank you, very much appreciated
2 Answers
Alexander Gyger
14,237 PointsWith your notation, be aware that your variable also without the "$" is still a jQuery object. Usually if you make a jQuery object variable it's good practice to use a "$" at the beginning of it to make it readable.
These are both jQuery objects
var $searchField = $('#search');
If you write it like this:
var searchField = $('#search');
they are both still jQuery objects. But without the "$", the variable could be misread in a long code as a regular JavaScript variable.
If you want to use it as a regular variable (just a string) like i did in my solution, you could have added the ".val()" already here
var searchTag = $('#search').val();
This is now just a regular non-jQuery string variable.
Ellis Briggs
11,108 PointsI believe the first one just helps show that it is a Jquery object instead of a normal JS one.
But they should work the same
cyber voyager
8,859 PointsThank you very much, Ellis.
cyber voyager
8,859 Pointscyber voyager
8,859 PointsThanks for the reply Alexander