1

Possible Duplicate:
Valid Characters for JavaScript Variable Names

I realize that a lot of js frameworks use $ as the short-hand like in jQuery. Is there any other symbol available in javascript, so that I can assign:

$ = function(){console.log('working')}
Community
  • 1
  • 1
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
  • 5
    Any letter is acceptable, even the "weird" ones like `é` or `ß`, so you have quite a lot to choose from. For example `卐 = function(){console.log("hey")}` – Deestan Dec 27 '12 at 15:07
  • 1
    @Deestan - Cool example. But note you can't use *any* letter for a variable name. – techfoobar Dec 27 '12 at 15:09
  • @techfoobar Are you sure? If so, which letters aren't allowed? The rule is "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)”." – Deestan Dec 27 '12 at 15:12
  • 3
    @techfoobar You can't use any *character*, but you *can* use any *letter* (see the linked duplicate for the generous definition of a Unicode "letter"). – apsillers Dec 27 '12 at 15:13
  • 1
    @apsillers - Ahh.. ok. I was wondering. Because as is obvious, we can't use things like `.`, `/`, `#`, `.` etc. for variable names. :-) – techfoobar Dec 27 '12 at 15:15

2 Answers2

4

Aside from the dollar sign ($) and underscore (_), I don't think there are any "basic" characters that aren't taken by basic operations or string delimiters.

Edit:

Basically, this means you'll have to give your short-hand a different name if you expect your code to be used in combination with jQuery / underscore.js

However, like Deestan mentioned in the comments, you can use (pretty much) any ASCII letter as variable name, as long as it isn't taken. Then, it's just a matter of convenience.
Accents like û are relatively easy to type. The more elaborate characters could require their ASCII codes to be entered manually, which kind-of defeats the purpose of a shorthand alias.

For example, while impossible to type, this is valid js:

.

Ḣ̶̤̟͍ͦe̱̺̺̮̗̼̖ͫͭ͜͟ͅ ͚͖͙̻̈́̏ċ̨̟̖͙͈̲͇͙̬̲ͩͭ́́́o͎͓̘̭̱̩ͮ͂ͯ̿ͩͨ͂́̚̕m̬͎̜̪͋͛̏̇̊̆̋̉̒͞ę̝͕̯̯̗͋ͩ̿̈́͒̓̾͝s̸͔̜͇̔͊͢ = 'Some text';

.

(No syntax formatting on it since that breaks the effect)

Community
  • 1
  • 1
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
2

Most libraries have a noConflict variable too. So, like in jQuery, you could use jQuery.fn instead of $.fn if you wanted.

There's another big library that uses _ and it's called underscore too. Never thought about it too much. But you should always think of collision when you're using common variables in global code.

Mutahhir
  • 3,812
  • 3
  • 21
  • 26