2

I don't grok the idea/purpose/use of Javascript $, as in

function $(id) { return document.getElementById(id); }

Could someone please explain or point me at an explanation?

Thanks!

-- Pete

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51
  • 1
    had to google grok. That's a new one to me. – Doug Chamberlain Jan 31 '11 at 21:16
  • 2
    `$` is just a variable name. It can be confusing at first blush since many other languages use '$' to denote a variable... but in the JS case it can be the whole name. Plus function names are also variables which means you can have `var $='hello';` or `function $() {}` or even `var $=function() {};` (2nd and 3rd result in the same) [helpful primer](http://javascript.infogami.com/Javascript_in_Ten_Minutes) – Rudu Jan 31 '11 at 21:18
  • 1
    http://stackoverflow.com/questions/846585/can-someone-explain-the-dollar-sign-in-javascript – Anil Namde Feb 01 '11 at 14:37

5 Answers5

8

When you see JavaScript code that involves lots of $(foo) function calls, it's probably using either the jQuery or the Prototype web development frameworks. It's just an identifier; unlike a lot of other languages, identifiers (function and variable names) can include and start with "$".

Pointy
  • 405,095
  • 59
  • 585
  • 614
7

In your code it is the name of a function.

function $(id) { return document.getElementById(id); }
$("my_id")

function myfunc(id) { return document.getElementById(id); }
myfunc("my_id")

Two functions, two different identifiers.

user113716
  • 318,772
  • 63
  • 451
  • 440
2

Most commonly this is used by JQuery - specifically, JQuery creates an object with a reference of $ which has various methods to simplify page manipulation.

It's technically possible for anything to attach a class to $

Basic
  • 26,321
  • 24
  • 115
  • 201
2

It's just the name of a function called $(). The dollar sign ($) is a valid character for identifiers in JavaScript. jQuery, for example, uses it as a shorthand alias for the jQuery() function.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
2

There are two main reasons to use $ as a function name, especially in frameworks like jQuery:

  • It's short but distinctive - you're going to use it all over the place, so you don't want it to take up too much space.
  • When used as a DOM element selector, the function and its parameters together kind of look like a Perl/PHP/Java properties variable - and it kind of works like that as well, since the main purpose is to do something with the selected DOM elements.
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 3) *Everyone else* does it. (I'm glad `_()` hasn't caught on yet) I find the 2nd argument weak because `var $foo` or `baz.$quux` isn't generally accepted. I don't see how the DOM somehow relates more to Perl/PHP/Java (or where Java uses `$` except for generated classes) than just, uhm, HTML. –  Jan 31 '11 at 22:12
  • 1
    @pst: the point is that what matters is not the function itself but the elements it returns, because that's what you really work with. As for Java using $, it appears a lot in the form ${varname} (which is really originaly from unix shell scripts), not in the language itself but in various related file formats like ant scripts and spring application contexts, and in the JSTL expression language. – Michael Borgwardt Jan 31 '11 at 23:06