I am reading a book on jQuery and I’ve seen in a few code examples in the book where the author will declare variables with a preceding $. My question is simply whether or not the $ is significant in itself or is it simply a convention/tradition to broadcast immediately to anyone who sees the variable that’s its a jQuery object and ‘not’ a regular JavaScript object?
If I declare two variable like this using jQuery:
$jQueryvar = $(‘body’);
jQueryvar = $(“body”);
Is there any difference between the two other than the $ being a part of the identifier. If there is a difference is it even possible to assign a jQuery object to a regular JavaScript object without using the preceding $ character?
I think I’ve got my question across. It seems difficult to word for some reason but if anyone can help clear this up that would be great. Thanks.
Pre-pending a variable with $ basically allows us to see what is a jQuery object and whats a standard JavaScript variable, developers including myself do this now as it makes it a lot faster to debug code that mixes between the jQuery scope and the standard element scope.
Overall there is no difference between the two apart from what they mean to us as developers.
That was what I was hoping you would say. Every clever idea actually. I like it. No significance to the behavior of the script but instantly recognizable as a jQuery object versus a JavaScript object to anyone who knows. Very cool!!