Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't ◎ܫ◎ and ☺ valid JavaScript variable names?

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 ◎ܫ◎, ♨_♨ and don't?

EDIT: Test it out in your browser on JSFiddle. I've tested it in Internet Explorer 9, Chrome, Firefox, and Opera. So far, it seems to only work in Internet Explorer 9. (I don't know about Internet Explorer 8 and below.) Let me know if it works in another browser.

like image 727
Peter Olson Avatar asked Sep 17 '11 00:09

Peter Olson


People also ask

What variable names are not allowed in JavaScript?

You can't use a number as the first character. The rest of the variable name can include any letter, any number, or the underscore. You can't use any other characters, including spaces, symbols, and punctuation marks. As with the rest of JavaScript, variable names are case sensitive.

What makes a variable name not valid?

Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name. Variable names may not be longer than 32 characters and are required to be shorter for some question types: multiselect, GPS location and some other question types.

Which is a valid JavaScript variable name?

The general rules for constructing names for variables (unique identifiers) are: Names can contain letters, digits, underscores, and dollar signs. Names must begin with a letter. Names can also begin with $ and _ (but we will not use it in this tutorial)


1 Answers

ಠ_ಠ 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.

like image 162
Michael Madsen Avatar answered Oct 13 '22 12:10

Michael Madsen