I was wondering if someone could explain the difference in caching elements in JQuery. One with the dollar symbol and one without.
Here is an example:
var cached = $('.someElement');
var $cached = $('.someElement');
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no difference. Both are cached, only the name of the variable is different.
Both
cachedand$cachedare valid Javascript variables:The $variable syntax is just often used to indicate the variable contains a jQuery object, not another type (string, integer, DOM Element, …). It’s a kind of Hungarian notation, but it’s just a convention amongst programmers. Nothing Javascript or jQuery imposes.
When people talk about caching a jQuery variable, they mean only doing the lookup once: