Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a javascript script, why so many $ (dollar signs)?

Tags:

javascript

I am trying to decipher a .js script and I am finding it filled with $ throughout? Is there any reason to use this? I'm pretty new to JavaScript.

like image 838
bitbitbot Avatar asked Sep 08 '25 12:09

bitbitbot


2 Answers

I think you are reading a JavaScript library famously known as jQuery (or possibly another library). The $ is just a short form for a namespace or use as an identifier.

You can think of it like this jQuery('p') to select all the paragraphs on a page, or for short form you can just write $('p').

Here is a link for jQuery tutorials/docs jQuery

Here is a list of standards section 7.6 describes it in detail ECMA Standard

like image 86
Spooks Avatar answered Sep 11 '25 01:09

Spooks


A number of libraries have used $ as their primary symbol. It's nothing to do with JavaScript per se, but it's a short distinctive symbol and so libraries have tended to glom onto it. (You can start an identifier with $ in JavaScript, and identifiers can be one character long, so $ is a valid identifier, just like a or x.)

I know of at least three libraries that use $ for something:

  • jQuery - It's the all purpose function for jQuery, an alias of the jQuery function; more here.
  • Prototype - It's Prototype's replacement for document.getElementById, more here. Prototype also has $$, which is for looking things up via CSS selectors.
  • MooTools - Same use as Prototype (because MooTools is either "inspired by" or "forked from" Prototype, some years back, depending on who you ask), more here. And like Prototype, it has $$.
like image 43
T.J. Crowder Avatar answered Sep 11 '25 03:09

T.J. Crowder