tags:

views:

1308

answers:

8

Hello to all,

Just wondering if anyone can point me to a webpage detailing the valid characters that can be used for naming a JavaScript variable.

I want to create a small 'extension library' for my non-javascript users here at work (who all seem to be squeamish when it comes to the language). I love how jQuery and Prototype have both used the '$' dollar sign, and since I use jQuery, I'm looking for another nice one character symbol to use.

I realize that I could just test out a number of characters, but I'm posing this question to the JavaScript Sensi's of the world to give me advice about what characters (even if valid) would be a bad idea to use (perhaps for future integration with another popular library).

Thank you,

Richard

+8  A: 

I think you want the Ecma 262 spec:

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

There's a section on page 15 which defines what identifier syntax can be.

Benj
+1 for the link to ECMA !
Cerebrus
Thank you. I found the information from within the article. Apparently, you can only use $ or _. Richard
Richard Clayton
+1 For pointing to the specification.
Gumbo
A: 

A good one

What are the rules for naming JavaScript variables?

rahul
This is the same article as the one Cerebus linked to, and it doesn't detail the special characters that can be used. I appreciate your help though.
Richard Clayton
A: 

How about this article at CodeLifter.com ?

Cerebrus
Thank you for the link. I came across this article while searching for the answer. Unfortunately, this is a "best practices" explanation and doesn't include anything about using special characters.
Richard Clayton
+4  A: 

Basically, in regular expression form: [a-zA-Z_$][0-9a-zA-Z_$]*. In other words, the first character can be a letter or _ or $, and the other characters can be letters or _ or $ or numbers.

Anthony Mills
I'm awarding the answer to Anthony. I looked up the ECMA standard Benj provided and found out that you can only use the _ or $. Thank you all for your help, though I'm saddened by this fact!
Richard Clayton
To further answer your question, the other character would be the underscore, as in http://documentcloud.github.com/underscore/ ... or you can always use something like `$rc` ... :)Prototype, for instance, uses `$$`, and the Microsoft Ajax Library uses $ with short words.
Anthony Mills
+3  A: 

Javascript Variables

You can start a variable with any letter, $, or _ character. As long as it doesn't start with a number, you can include numbers as well.

Start: [a-z], $, _

Contain: [a-z], [0-9], $, _

jQuery

You can use _ for your library so that it will stand side-by-side with jQuery. However, there is a configuration you can set so that jQuery will not use $. It will instead use jQuery. To do this, simply set:

jQuery.noConflict();

This page explains how to do this.

EndangeredMassa
This is absolutely correct, but I gave the answer to Anthony who answered .02123413124 milliseconds before you. Sorry.
Richard Clayton
+2  A: 

Javascript variables can have letters, digits, dollar signs ($) and underscores (_). They can't start with digits.

Usually libraries use $ and _ as shortcuts for functions that you'll be using everywhere. Although the names $ or _ aren't meaningful, they're useful for their shortness and since you'll be using the function everywhere you're expected to know what they mean.

If your library doesn't consist on getting a single function being used everywhere, I'd recommend that you use more meaningful names as those will help you and others understand what your code is doing without necessarily compromising the source code niceness.

You could for instance take a look at the awesome DateJS library and at the syntatic sugar it allows without the need of any symbol or short-named variables.

You should first get your code to be practical, and only after try making it pretty.

Miguel Ventura
+5  A: 

Actually, ECMAScript says on page 15: That an identifier may start with a $, an underscore or a UnicodeLetter, and then it goes on (just below that) to specify that a UnicodeLetter can be any character from the unicode catagories, Lo, Ll, Lu, Lt, Lm and Nl. And when you look up those catagories you will see that this opens up a lot more possibilities than just latin letters. Just search for "unicode catagories" in google and you can find them.

Yuvalik
+1 - I will expand a little more on this
Anurag
+2  A: 

From the ECMAScript specification in section 7.6 Identifier Names and Identifiers, a valid identifier is defined as:

Identifier :: 
    IdentifierName but not ReservedWord

IdentifierName :: 
    IdentifierStart 
    IdentifierName IdentifierPart 

IdentifierStart :: 
    UnicodeLetter 
    $ 
    _ 
    \ UnicodeEscapeSequence 

IdentifierPart :: 
    IdentifierStart 
    UnicodeCombiningMark 
    UnicodeDigit 
    UnicodeConnectorPunctuation 
    \ UnicodeEscapeSequence 

UnicodeLetter 
    any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, 
    “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”. 

UnicodeCombiningMark 
    any character in the Unicode categories “Non-spacing mark (Mn)” or “Combining spacing mark (Mc)” 

UnicodeDigit 
    any character in the Unicode category “Decimal number (Nd)” 

UnicodeConnectorPunctuation 
    any character in the Unicode category “Connector punctuation (Pc)” 

UnicodeEscapeSequence 
    see 7.8.4. 

HexDigit :: one of 
    0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

which creates a lot of opportunities for naming variables and also in golfing. Let's try some examples.

A valid identifier could start with either a UnicodeLetter, $, _, or \ UnicodeEscapeSequence. A unicode letter is any character from these categories (see all categories):

  • Uppercase letter (Lu)
  • Lowercase letter (Ll)
  • Titlecase letter (Lt)
  • Modifier letter (Lm)
  • Other letter (Lo)
  • Letter number (Nl)

This alone accounts for some crazy possibilities - working examples. If it doesn't work in all browsers, then call it a bug, cause it should.

var ᾩ = "something";
var ĦĔĽĻŎ = "hello";
var 〱〱〱〱 = "less than? wtf";
var जावास्क्रिप्ट = "javascript"; // ok that's JavaScript in hindi
var KingGeorgeⅦ = "Roman numerals, awesome!";
Anurag