{Carrer web log}


Blog about web design & development

About the dollar sign ($) in JavaScript

Friday, October 15, 2010 { 10 Comments }

I’m writing this short post because I think that many people are puzzled by the $ sign in JavaScript. When I started using jQuery I thought that the $ sign have some super powers or do some Voodoo magic.

The truth is the dollar sign is "special" in JavaScript but unfortunately doesn’t do Voodoo magic.

Why the $ sign is special?

Because you can do this: var $ = "Hello World";

But you can’t do this: var £ = "Hello World"; or var @ = "Hello World"; or var # = "Hello World";

The $ sign is "special" because is treated like any normal letter (a,b,c …).

So the $ is cool sign that probably every keyboard have and is perfectly legal to use. Probably because of that John Resig decided to give him special role in JQuery and later the $ sign was implemented by Prototype and MooTools. How jQuery and the other libraries implement the $ is another issue which will not be discussed in this article.

Another characters like the ($) dollar is the underscore (_).

Quote from Standard ECMA-262 ECMAScript Language Specification


7.6 Identifier Names and Identifiers
Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications. An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1). The Unicode identifier grammar is based on both normative and informative character categories specified by the Unicode Standard. The characters in the specified categories in version 3.0 of the Unicode standard must be treated as in those categories by all conforming ECMAScript implementations.

This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.



What other characters(letters) are there?

Recently I discovered that you can use some small Ancient Greek letters Alpha, Beta, Delta and others (α, β, δ) can be used.

So var α = "hello world"; is ok. Unfortunately there is no Alpha (α ) sign on my keyboard.

Potentially this Greek letters can be used to avoid "conflicts" or in some minification tools but that is probably another topic.

Did I missed something?

Update: I was wrong that the the jQuery was first implement the $ sign. But my point was not who was the first or last but the explain that the $ is just like any other letter in JavaScript.



10 Responses to “About the dollar sign ($) in JavaScript”

  1. // Blogger MichalBe // 10/15/2010

    "Probably because of that John Resig decided to give him special role in JQuery and later the $ sign was implemented by Prototype and MooTools."

    Hmm, I always thought that prototype was created in February 2005 and jQ in January 2006.  

  2. // Blogger Vladimir // 10/15/2010

    @MichalBe: Thanks, you are right.  

  3. // Blogger Rob Mills // 10/15/2010

    Thank you. Always was curious about that myself.  

  4. // Anonymous Tom // 10/15/2010

    Thank you. I was always puzzled about this. It is useful to be able to use the $ sign to show that some variables might be special in some way, e.g., globals  

  5. // Blogger Poetro // 10/15/2010

    "Potentially this Greek letters can be used to avoid "conflicts" or in some minification tools but that is probably another topic."

    The problems with the Greek letters and minification is that they comsume 2 bytes and may not be compatible with every browser. It is better to be safe with 2 ASCII character variable names, as they also consume 2 bytes then any other character, and it could also be better compressed with gzip.  

  6. // Anonymous Anonymous // 10/18/2010

    Interesting article.

    However, some corrections though (not to what the $ is for in JavaScript).

    - Is not 'woody' magic, is 'Voodoo' magic.

    - You spelled "java script" and then "javascript", I think is 'JavaScript'.

    - Be consistent. Try typing 'jQuery' right every time (there's a JQuery in your text). I assume you're a programmer/coder... you can't type wrong when programing, otherwise your application won't work.

    Thanks.  

  7. // Blogger Vladimir // 10/18/2010

    @Anonymous: First, thank you! I really appreciate your comment. I will update this post with yours suggestions. I usually make grammatical and other mistakes when I'm doing fast post like this. Other thing I'm little dyslexic and I'm not coming from English speaking country. But that doesn't excuse me at all. I should be definitely more careful when posting. Making "cheap" typos make my blog post and me not professional and not serious so I will try to avoid that.  

  8. // Blogger Jason // 10/18/2010

    The dollar sign is specia, but it should be only used for machine generated output.  

  9. // Blogger Vladimir // 10/18/2010

    @Jason: That was in the previous version of ECMA-262 (Archive Version) http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm. From Section 7.6: "The dollar sign is intended for use only in mechanically generated code."

    Today the latest version of this Ecma Standard dosen't say anything about "mechanically generated code". Here is the PDF of latest Ecma Standard http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf (chapter 7.6)  

  10. // Anonymous Anonymous // 10/19/2010

    @Vladimir: OFFTOPIC: No problem amigo, glad I could help.

    FYI, English is not my first language either, it's Spanish, but I try to make sure that whatever I write/type is as clean and well written as possible.

    Nos vemos.  

Post a Comment

<< Home

RSS IconTwitter icon Twitter icon Twitter icon

About Me <<<

Name: Vladimir Carrer
vladocar [at] gmail.com
Location: Verona, Italy
I'm a web designer, developer, teacher, speaker, generally web addicted ...

My projects <<<

§§Previous Posts <<<

Hand Drawn Icons
Photoshop Wireframing Kit Ad
 

Other Profiles <<<

View Vladimir Carrer's profile on LinkedIn

Content is licensed under a Creative Commons Public Domain License