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).

Though unicode letters are acceptable in variable names, using unicode in code can be problematic. I would suggest not using them in variable names if you can get by without them.
–
Gary S. WeaverMay 10 '13 at 19:01

I’ve also created a tool that will tell you if any string that you enter is a valid JavaScript variable name according to ECMAScript 5.1 and Unicode 6.1:

P.S. To give you an idea of how wrong Anthony Mills' answer is: if you were to summarize all these rules in a single ASCII-only regular expression for JavaScript, it would be 11,236 characters long. Here it is:

Technically, you can use any "Unicodeletter". Letters like ä or π are valid because they are part of the unicode character set and are not, technically, symbols like $, & or @. I would recommend avoiding all such characters because most of us who do not use an extended charset will have a difficult time recalling which Alt+ keys to pound out to get the appropriate character.
–
RLHAug 3 '11 at 20:24

3

@Timwi Because copy & paste programming is so preferable. If any library makes me use a unicode character to invoke a function, the only key I'm going to press is DELETE.
–
CypherOct 23 '13 at 22:31

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.

Note: This answer is not technically correct. You can use Unicode characters in identifiers. But don't do it. Encodings get screwed up all the time. Keep your code in the 32-126 ASCII range where it's safe.

I would probably plot to assassinate a co-developer who used unicode characters in his label names. /barely ironic speech
–
Erik ReppenSep 16 '11 at 18:54

15

Anthony, would you kindly delete this answer please, now that we know it's wrong?
–
romkynsFeb 24 '12 at 21:01

31

@Timwi - I've added a note as to why my answer is not technically correct. And I agree with Erik Reppen that I would want to kill anyone who took advantage of the "real" answer. And we'll just have to agree to disagree that I'm everything that's wrong with Stack Overflow. :)
–
Anthony MillsJul 5 '12 at 19:23

24

@Tchalvak For code that just you are using, that's probably fine, using Ʒ as your main library name. (Oh, you thought that was a 3? So sorry, it's actually U+01B7 Latin Capital Letter Ezh! Or was it З, Cyrillic Capital Letter Ze?) If you're going to write a library that might be used by other people, though, it's probably best to stick to ASCII.
–
Anthony MillsAug 9 '12 at 14:21

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.

In English: It must start with a dollar sign, underscore or one of letters in the 26-character alphabet, upper or lower case. Subsequent characters (if any) can be one of any of those or a decimal digit.

This is more difficult to express in English, but it is conceptually similar to the older syntax with the addition that the letters and digits can be from any language. After the first character, there are also allowed additional underscore-like characters (collectively called “connectors”) and additional character combining marks (“modifiers”). (Other currency symbols are not included in this extended set.)

JavaScript 1.5 and later also allows Unicode escape sequences, provided that the result is a character that would be allowed in the above regular expression.

Identifiers also must not be a current reserved word or one that is considered for future use.

There is no practical limit to the length of an identifier. (Browsers vary, but you’ll safely have 1000 characters and probably several more orders of magnitude than that.)

*n.b.This Perl regex is intended to describe the syntax only — it won’t work in JavaScript, which doesn’t (yet) include support for Unicode Properties. (There are some third-party packages that claim to add such support.)

I can't seem to get this regex to actually work. "test".match(/^[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*$/) === null even though "test" is a valid JS variable name
–
David MurdochFeb 2 '12 at 2:08

Sorry, but JavaScript doesn’t support this regex. I have added a note to clarify.
–
danortonFeb 3 '12 at 0:53

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:

The accepted answer would rule out a lot of valid identifiers, as far as I can see. Here is a regular expression that I put together which should follow the spec (see chapter 7.6 on identifiers). Created it using RegexBuddy and you can find an export of the explanation at http://samples.geekality.net/js-identifiers.

This regex is not a valid JS regex. I think you meant: ^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*$. Now even with the correction I can't seem to get this regex to actually work. "test".match(/^[\p{L}\p{Nl}$_][\p{L}\p{Nl}$\p{Mn}\p{Mc}\p{Nd}\p{Pc}]*$/) === null even though "test" is a valid JS variable name
–
David MurdochFeb 2 '12 at 2:11

No, I'm pretty sure I meant what I wrote :) The question, as I understood, just asked for what is a valid javascript function name, not how a regex for that would be in specifically javascript. I made it in RegexBuddy and I'm using it in PHP on the sample page I link to. Works great, and test is accepted as well.
–
SvishFeb 2 '12 at 8:46

2

@DavidMurdoch I’ve written a 11,335-character JavaScript-compatible regular expression that can be used to validate identifiers (aka. variable names). See my answer.
–
Mathias BynensFeb 18 '12 at 1:48

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.

Well, then isValidVarName('aler(t') becomes true. And isValidVarName('_;;;') stays true. But you could check at the start if it matched something like /[;,\(\)]/ but then you can still execute _=location="#!?" so you add = to the list but you can still execute '_\ndelete foo' (which passes the test as a valid variable name) so you have to exclude \ns and \rs and maybe some unicode newline? But ` $ ` isn't a valid identifier so you have to exclude all whitespace... It's a losing battle. I think that's as far as I can go against myself with if(/[;,\(\)=\s]/.exec(name))return!1
–
1j01Jul 21 '14 at 5:03

Here is one quick suggestion for creating variable names. If you want the variable not to conflict when being used in FireFox, do not use the variable name "_content" as this variable name is already being used by the browser. I found this out the hard way and had to change all of the places I used the variable "_content" in a large JavaScript application.

Can you prove this with some source code which fails? It doesn't appear to do anything in Firefox.
–
toothbrushFeb 21 '14 at 17:57

Here is a jsfiddle that alerts when the variable "_content" is not "undefined" and when "_content" is set by FireFox, it is set to equal "window.content" jsfiddle.net/R2qvt/3
–
DanBrianWhiteFeb 21 '14 at 20:50