0

I found an JavaScript file in a website using a variable like this:

var $variable

What kind is it? Thanks, DGM

DGM.-
  • 151
  • 3
  • 13
  • It's nothing special but maybe it use jquery and indicate that the value is jquery object, I use variables like that in my code. – jcubic Mar 30 '13 at 16:38
  • 2
    See **[this answer](http://stackoverflow.com/a/553734/1626250)** to **[this question](http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign)**. – Mike Rockétt Mar 30 '13 at 16:40
  • 1
    JavaScript doesn't have "kinds" of variables. –  Mar 30 '13 at 16:42

4 Answers4

3

$ 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.

David G
  • 94,763
  • 41
  • 167
  • 253
1

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".

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

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.

Angus Comber
  • 9,316
  • 14
  • 59
  • 107
0

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.

SteveP
  • 18,840
  • 9
  • 47
  • 60