3

Possible Duplicate:
Valid Characters for JavaScript Variable Names

I know that the ecmascript spec 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 any technical difference?

Community
  • 1
  • 1
Billy Moon
  • 54,763
  • 23
  • 128
  • 231
  • 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 / 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.

kenorb
  • 137,499
  • 74
  • 643
  • 694
  • 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 are no "real" technical differences. Almost all unicode letters are allowed as variable names in Javascript, check out this great writeup for more info.

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

Marcus Olsson
  • 2,449
  • 18
  • 35
0

Yes you can.

Here are some links that support that claim

http://mathiasbynens.be/notes/javascript-identifiers

http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html

Clyde Lobo
  • 9,033
  • 7
  • 35
  • 61