Monday, May 20, 2024
143
rated 0 times [  144] [ 1]  / answers: 1 / hits: 51258  / 13 Years ago, sat, september 17, 2011, 12:00:00

I noticed that in Internet Explorer (but, unfortunately, not in the other browsers I tested), you can use some Unicode variable names. This made my day, and I was absolutely delighted that I could write fun Unicode-laden code like this:


var ктоείναι草泥马 = "You dirty horse.",
happy☺n☺mat☺p☺eia = ":)Yay!",
ಠ_ಠ = "emoticon";

alert(ктоείναι草泥马 + happy☺n☺mat☺p☺eia + ಠ_ಠ);

For some reason, though, ◎ܫ◎, ♨_♨ and are not valid variable names.


Why do ಠ_ಠ and 草泥马 work, but not ◎ܫ◎, ♨_♨ or ?


More From » naming-conventions

 Answers
97

ಠ_ಠ
and 草泥马 only contain letters used in actual alphabets; that is, ಠ
is a symbol from the Kannada alphabet, and 草泥马 consists of Chinese characters.



◎ and ☺, however, are purely symbols; they are not associated with any alphabet.



The ECMAScript standard, chapter 7.6 (which all the browsers except Internet Explorer are following), states that an identifier must start with one of the following.




  • a Unicode letter

  • $ or _

  • followed by a unicode escape sequence.



The following characters of an identifier must be one of the following.




  • any of the characters permitted at the start

  • a Unicode combining mark

  • a Unicode digit

  • a Unicode connector punctuation

  • a zero-width-non-joiner

  • a zero-width joiner



IE goes beyond the standard and is permissive enough to allow some symbols, such as ☺.



There’s 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.


[#90054] Thursday, September 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradenc

Total Points: 75
Total Questions: 96
Total Answers: 129

Location: Burundi
Member since Thu, Feb 10, 2022
2 Years ago
;