2

Famous javascript libraries,

  1. jQuery
  2. prototypejs
  3. mootools
  4. scriptaculous

all uses $. What's the significance of $ ?

and also, even php uses it to declare variables. I am sure some other languages uses it too.

Jashwant
  • 26,663
  • 15
  • 65
  • 99
  • http://stackoverflow.com/questions/6847041/reading-a-javascript-script-why-so-many-dollar-signs http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign http://stackoverflow.com/questions/3107543/what-is-the-symbol-used-for-in-javascript – Josh Lee Jul 30 '12 at 15:21
  • The significance of `$` in JavaScript is that you can use it in your identifiers (variable names, parameters, etc.). Identifiers may consist of letters, digits, and the `$` and `_` characters. – Šime Vidas Jul 30 '12 at 15:22
  • @JoshLee, How did you search that ? I thought it would have been asked earlier but couldnt find it. – Jashwant Jul 30 '12 at 15:54

2 Answers2

8

It's just a character that can be part of identifiers in JavaScript. No special significance outside of the fact that it's distinctive-looking. It's easy to recognize when you're scanning over code.

Now, in other languages, it can (and does) mean all sorts of things. The term sigil has come into fashion when referring to "special" characters that have syntactic significance in a programming language, specifically as modifying the semantics of a reference to a variable. In JavaScript, I would not use that term for "$" because it doesn't mean anything in particular to the interpreter.

edit — as noted in a comment, "$" has a role in JavaScript regular expression syntax, but I wouldn't consider that related in any way to its use in variable names. (Maybe somebody would; I don't know.)

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • It does have a special meaning in JavaScript's RegExp grammar, though. – Šime Vidas Jul 30 '12 at 15:25
  • @ŠimeVidas oh yes, that's true - I'll add that (though it's of course a completely separate thing) – Pointy Jul 30 '12 at 15:27
  • Strange ! everyone uses it . I thought there's some historical significance associated with it but couldnt find. I find it easier to type `$` – Jashwant Jul 30 '12 at 16:55
2

In Javascript, $ is just another character that can be used in variables. jQuery decided to use that as a short name for the jQuery() function so it will take up less space. Identifiers may consist of letters, digits, and the $ and _ characters

In other languages:

Perl $ in front of a variable means scalar, @ means array, % means hash and & means function. PHP $ means what follows is a variable which could be an object reference. Commodore basic: a trailing $ means it is a string variable. (You didn't need to know that.)

In PHP and Perl, the syntax reveals the details of the identifier so the program is easier to follow. name() name[] $name(), etc. all mean different things which are obvious from the syntax.

dadinck
  • 1,090
  • 1
  • 7
  • 8