javascriptnaming-conventions

What is the purpose of the dollar sign in JavaScript?


The code in question is here:

var $item = $(this).parent().parent().find('input');

What is the purpose of the dollar sign in the variable name, why not just exclude it?


Solution

  • A '$' in a variable means nothing special to the interpreter, much like an underscore.

    From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.

    The dollar sign function $() in jQuery is a library function that is frequently used, so a short name is desirable.