Good day!

Increasingly, in js scripts I come across variable declarations using the dollar sign, i.e. instead of a simple declaration var A, I see var $ A , and then by the access code for this variable, then so is $ A , then so is $ (A) . Here, for example, a piece of the script:

var forms = function(){ $('.js_form').each(function(){ var $form = $(this), $customField = $form.find('[data-error]'), customFieldRulls = {}; var zIndexs = function(){ setTimeout(function(){ $('label.error').each(function(i){ $(this).css({'z-index':'50'-i}) }); },200) }; 

Both ordinary variables and "variables with the dollar" are used. I do not understand what the difference between them? Is it just done for convenience, or does it provide something extra from a functional point of view?

  • one
    If we talk in context jQuery, in most cases, thus show that the variable is exactly the jQuery object . It carries only a semantic load and is more convenient for understanding what you work with. - Deonis

2 answers 2

I can not be sure of all 146%, but there is a feeling that the dollar sign here indicates the fact that the value of the variable was obtained using the jQuery selector. While the other, the usual js-variables go without this prefix. In principle, it looks quite logical - js is a language with dynamic typing, and it is not always possible to understand by the name of the variable that it is stored in it. Therefore, the dollar prefix facilitates this understanding.

However, regardless of whether my assumption is true or not, the ad with the dollar does not carry any new functionality - in js it is the same symbol that can be used in the names of identifiers as other

  • one
    101% already have it. - Sergiks
  • In my convention, $ XXX is any sample of items. - etki Nov.
  • Thank you all, now it will be easier to perceive these scripts. )) - maler1988

Not certainly in that way. Based on the latest jQuery specifications:

 <div id="selector"></div> function ready() { var selector = $('#selector'); var selector = $('#selector').context; var $selector = $('#selector'); } window.onload = ready; 

In the first case, the selector returns [object Object]

In the second [object HTMLDocument] (for completeness)

And in the third [object HTMLDivElement]

In short, the third option will work as document.getElementById ('selector'); on pure js.

And there already see for yourself who needs what and how to work with.