-5

I wonder that why javascript can use $ (dollar sign) as function name but can't use pound sign (£). Is there a logic or limitation for that?

edit: This question not duplicate of given question. and theres no source for the answer in this site. This is like "hey this is javascript catalog find you're looking for in it"

  • 1
    Take a look here: http://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names – Bubble Hacker Jun 29 '16 at 07:43
  • 1
    ***LAUGHING OUT LOUD*** that you should call £ the "euro sign" given that it's the British Pound Sterling sign (the currency of the UK) and the UK **just** voted to leave the European Union (where the Euro [€] is the currency in most countries). BTW, "dollar" has two ells in it. – T.J. Crowder Jun 29 '16 at 07:43
  • 1
    @Nina - No particular reason to reopen it, but I don't think "why" is a duplicate of "what". – T.J. Crowder Jun 29 '16 at 07:49
  • the `$` was intended to be a prefix to identify "mechanically generated code" in early ES, but that convention (and idea) never really took off.. – dandavis Jun 29 '16 at 07:50
  • @T.J.Crowder sorry my mistake:) It's clear im not using both of those banknotes:) editing. – Güven Altuntaş Jun 29 '16 at 07:53
  • @GüvenAltuntaş: Hey, it was a good laugh. ;-) – T.J. Crowder Jun 29 '16 at 07:58
  • 1
    some more info to [*mechanically generated code*](http://stackoverflow.com/questions/16454175/why-is-the-dollar-sign-no-longer-intended-for-use-only-in-mechanically-generate) – Nina Scholz Jun 29 '16 at 08:14

2 Answers2

2

No, there's no particular logic to it, it's just the rules that Brendan Eich decided on during those fraught ten days in May 1995. The full rules are here. Eich just decided that allowing $ in identifier names would be handy. He also allowed _ (which is more common in programming languages), as well as the usual set of English letters and numbers (where numbers can't appear in the first character).

At one stage, an attempt was made in the specification to retcon $ as "intended for use only in mechanically generated code." This language appeared for the first time in ECMAScript 2nd edition (pdf), not being present in the 1st edition (pdf). It remained in the 3rd edition (pdf), and then disappeared in the 5th edition (there was no accepted 4th edition). This question and its answers address this, apparently it was an attempt to use a convention that originated in Java. But people glommed onto $ (not least John Resig with jQuery and Sam Stephenson with PrototypeJS), so, well, that ship has long-since sailed, hence dropping that language from the 5th edition spec.

Ultimately, a wide range of Unicode characters were allowed (for instance, fairly famously, ಠ_ಠ is a valid JavaScript identifier), but that was much later, in the ECMAScript 3rd edition (implemented by Mozilla in JavaScript 1.5).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • is there a simple rule that determines which unicodes are allowed? – dandavis Jun 29 '16 at 07:54
  • @dandavis: There is *now*. Unicode has been updated with two properties for characters: `ID_Start` and `ID_Continue`. JavaScript allows you to use any character with the `ID_Start` property at the beginning of an identifier name, and any character with `ID_Continue` after that in an identifier name. Earlier versions of the JavaScript specification referring to earlier versions of Unicode and [had more complex rules](http://ecma-international.org/ecma-262/5.1/#sec-7.6) because `ID_Start` and `ID_Continue` didn't exist yet. – T.J. Crowder Jun 29 '16 at 07:56
0

Quote from Standard ECMA-262 ECMAScript Language Specification

7.6 Identifier Names and Identifiers

Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications. An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1). The Unicode identifier grammar is based on both normative and informative character categories specified by the Unicode Standard. The characters in the specified categories in version 3.0 of the Unicode standard must be treated as in those categories by all conforming ECMAScript implementations.

This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331