3

Possible Duplicate:
Valid Characters for JavaScript Variable Names

I know that the ECMAScript specification says I can use µ (Greek lowercase Mu), and I have tested on a couple of browsers, but is it rock solid? Will it work with all browsers? What could be the pitfalls?

Technically I don't expect it is any different than jQuery using $ as a variable name - is there a technical difference?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Billy Moon
  • 57,113
  • 24
  • 136
  • 237
  • 1
    Yes you can use µ, all unicode letters are allowed – this thread might be of interrest: http://stackoverflow.com/questions/1661197/valid-characters-for-javascript-variable-names – Marcus Olsson Aug 08 '12 at 11:35
  • 1
    This validator might also help: http://mothereff.in/js-variables#%E2%98%83 – Sudhir Bastakoti Aug 08 '12 at 11:38
  • 1
    More to the point: should you use it? I think not. What's wrong with `mu`? – stusmith Aug 08 '12 at 11:40
  • Please don't, my keyword can't input `µ` :( – xdazz Aug 08 '12 at 11:41
  • @xdazz what keyboard/os are you using? Can you not get it with `Alt Gr+m`? – Billy Moon Aug 08 '12 at 11:52
  • @Billy Moon: it is OS-dependent. For instance, I mapped my compose key to the Windows button, and have to do `compose key + m + u` (m and u in sequence) to get `µ`. French keyboards, for instance have a `µ` key for reasons only few can understand... It is not really a problem of not being able to input it, but rather a problem of not being practical to have a variable called `µ`. – nico Aug 08 '12 at 12:06

3 Answers3

5

Yes, you can use it.

In example:

var π = Math.PI;

as well as Unicode escape sequences.

Basically, the variable in JavaScript can be anything that's valid according to ECMAScript 5.1 and Unicode 6.1.

Here is some online JavaScript variable name validator.

For the references, this page is for documenting the differences between ES5 specification and the requirements for ECMAScript implementations in web browsers.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • I need to know if it will work in all browsers, including mobile devices, and server side javascript etc... not whether some characters can be used on some browsers. – Billy Moon Aug 08 '12 at 12:02
  • 1
    All browsers should have ECMAScript compability, so it should. If not, it's a bug then. Most of the mobiles using Safari or Opera, so they have the same implementations. So why it should work different. Why somebody should implement some limitations which are nowhere defined. If this works for IE, Firefox, Chrome and Safari, it'll work everywhere. – kenorb Aug 08 '12 at 12:29
1

No, there aren't any "real" technical differences. Almost all Unicode letters are allowed as variable names in JavaScript. Check out this great write-up for more information.

However, some Unicode characters use more bytes, so ASCII are perhaps the way to go anyway.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcus Olsson
  • 2,485
  • 19
  • 35
0

Yes, you can.

Here are references that support that claim

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61