I am a JavaScript learner and have been researching this matter, but with no success. What is the $
symbol used for in JavaScript besides regular expressions? Any resources or readings regarding this would be appreciated. Thanks.
-
2Duplicate: stackoverflow.com/questions/205853/…– Ben RoweCommented Jun 24, 2010 at 6:11
-
Perhaps you'll find this thread helpful. stackoverflow.com/questions/846585/…– jjnguyCommented Jan 27, 2012 at 5:43
3 Answers
It doesn't mean anything special.
But because $
is allowed in identifier names, many Javascript libraries have taken to using $
as the "central" interface to them, or at least as a shortcut for accessing their functionality.
For example, if you're using jQuery and you say $("div")
, this is a call to the $
function with argument "div". When you say $.post()
, it's calling the post
method on the $
object (Javascript is nice in that functions are first-class objects).
-
13The $ was actually included in the ECMA specification to distinguish machine generated code from human code .– NM.Commented Jun 24, 2010 at 8:09
-
I originally had this question as well. $ is a valid variable name as per the the Javascript Specification.... see MDN (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…) So the reason the nebie javascript developer has a problem understanding what $ is, is because you never get to see that $ was assigned as an object or function. Usually a base object as this answer describes. i.e. var $ = {}. If you drill down in the source code of the imported modules you'd eventually find it. Commented Apr 22, 2020 at 16:57
I became acquainted with it in JavaScript when I started using the Prototype framework. In Prototype, $
is simply the name of an often used function (very, very much simplified - a short for document.getElementById
). Personally, I like the terseness of it.
Afaik, it's not used for anything by the language itself.
For what it's worth, Douglas Crockford advises against using $
in the variable/function names you write:
Do not use $ (dollar sign) or \ (backslash) in names.
Adding another, rather opinionated, quote from Mr. Crockford's talk "And Then There Was JavaScript":
Dollar sign was added to the language specifically for use by code generators and macro processes, so if you have machines writing code then the machines need to be confident that the variables that they create will not conflict with variables that the humans are going to create. To distinguish them, we’ll allow the machines to use dollar sign. Some of the ninjas found out about that and thought oh, dollar sign, I can use dollar sign as a function name, so they’re out there doing that. And it looks stupid. I mean, look at a program with dollar sign.
-
1So is
$("someId")
the same asdocument.getElementById("someId")
?– BabikerCommented Jun 24, 2010 at 6:06 -
-
5@Babiker It depends on how
$
is defined (there is no function "$" defined within ECMAScript):function $ () { /* libraries do what they want */ }
– user166390Commented Jun 24, 2010 at 6:09 -
If you are asking why some variables and function names start with $
, then that is simply a convention when using jQuery and/or AngularJS.
In code that uses jQuery, $
is often used as a prefix for variables that contain jQuery selections.
e.g. var $container = $('.container');
.
In AngularJS, they use the $
prefix to mean "core Angular functionality". That way, you know which methods and services are added by the framework, and which are custom to your application.