Can someone explain the dollar sign in Javascript?

Keith Donegan picture Keith Donegan · May 11, 2009 · Viewed 387.8k times · Source

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?

Answer

cobbal picture cobbal · May 11, 2009

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.