I found an JavaScript file in a website using a variable like this:
var $variable
What kind is it? Thanks, DGM
I found an JavaScript file in a website using a variable like this:
var $variable
What kind is it? Thanks, DGM
$
is a regular symbol like any other legal one in JS and can be used as or part of a variable as well:
var $ = {};
It's also the identifier for the jQuery object. So that's why you'll normally see variables named that way that represent jQuery objects:
var $variable = $('#element');
There's also Underscore.js that uses the underscore symbol _
as its root object.
It is a completely normal variable, starting with the dollar sign - which has no special meaning in JavaScript. It is a valid identifier just as like the underscore.
Sometimes, variable names prefixed with $ indicate that they contain an object wrapper created by one of the libraries that use $
as a constructor (for example jQuery); in contrast to a "plain value".
Javascript identifiers must start with a letter, an underscore or a dollar sign. So it is just a variable.
The intended use of the $ sign was for code generators, but some libraries use it for their own purposes.
It is perfectly valid to use $ in javascript variable names. Personally, I try to avoid this as it can be confused with jquery objects or php variables which may have crept into the javascript by mistake.