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.


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


    EcmaScript 262, section 7.6 says names must start with $, _, or a Unicode letter, and after that may contain either those characters, or Unicode combining marks, Unicode digits, or Unicode connector punctuation (and a couple of format-control characters that are language specific.)

    So, the difference between allowed and not allowed identifiers in your cases is probably whether the leading character is considered a "letter".


    To quote Valid JavaScript variable names , my write-up summarizing the relevant spec sections:

    An identifier must start with $ , _ , or 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)”.

    The rest of the string can contain the same characters, plus any U+200C zero width non-joiner characters, U+200D zero width joiner characters, and characters in the Unicode categories “Non-spacing mark (Mn)”, “Spacing combining mark (Mc)”, “Decimal digit number (Nd)”, or “Connector punctuation (Pc)”.

    I've also created 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.3:


    PS If you were to summarize all these rules in a single ASCII-only regular expression for JavaScript, it would be over 9,000 characters long . Here it is:

    // ECMAScript 5.1 / Unicode v6.3.0:
    /^(?!(?:do|if|in|for|let|new|try|var|case|else|enum|eval|null|this|true|void|with|break|catch|class|const|false|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$)[x24A-Zx5Fa-zxAAxB5xBAxC0-xD6xD8-xF6xF8-u02C1u02C6-u02D1u02E0-u02E4u02ECu02EEu0370-u0374u0376u0377u037A-u037Du0386u0388-u038Au038Cu038E-u03A1u03A3-u03F5u03F7-u0481u048A-u0527u0531-u0556u0559u0561-u0587u05D0-u05EAu05F0-u05F2u0620-u064Au066Eu066Fu0671-u06D3u06D5u06E5u06E6u06EEu06EFu06FA-u06FCu06FFu0710u0712-u072Fu074D-u07A5u07B1u07CA-u07EAu07F4u07F5u07FAu0800-u0815u081Au0824u0828u0840-u0858u08A0u08A2-u08ACu0904-u0939u093Du0950u0958-u0961u0971-u0977u0979-u097Fu0985-u098Cu098Fu0990u0993-u09A8u09AA-u09B0u09B2u09B6-u09B9u09BDu09CEu09DCu09DDu09DF-u09E1u09F0u09F1u0A05-u0A0Au0A0Fu0A10u0A13-u0A28u0A2A-u0A30u0A32u0A33u0A35u0A36u0A38u0A39u0A59-u0A5Cu0A5Eu0A72-u0A74u0A85-u0A8Du0A8F-u0A91u0A93-u0AA8u0AAA-u0AB0u0AB2u0AB3u0AB5-u0AB9u0ABDu0AD0u0AE0u0AE1u0B05-u0B0Cu0B0Fu0B10u0B13-u0B28u0B2A-u0B30u0B32u0B33u0B35-u0B39u0B3Du0B5Cu0B5Du0B5F-u0B61u0B71u0B83u0B85-u0B8Au0B8E-u0B90u0B92-u0B95u0B99u0B9Au0B9Cu0B9Eu0B9Fu0BA3u0BA4u0BA8-u0BAAu0BAE-u0BB9u0BD0u0C05-u0C0Cu0C0E-u0C10u0C12-u0C28u0C2A-u0C33u0C35-u0C39u0C3Du0C58u0C59u0C60u0C61u0C85-u0C8Cu0C8E-u0C90u0C92-u0CA8u0CAA-u0CB3u0CB5-u0CB9u0CBDu0CDEu0CE0u0CE1u0CF1u0CF2u0D05-u0D0Cu0D0E-u0D10u0D12-u0D3Au0D3Du0D4Eu0D60u0D61u0D7A-u0D7Fu0D85-u0D96u0D9A-u0DB1u0DB3-u0DBBu0DBDu0DC0-u0DC6u0E01-u0E30u0E32u0E33u0E40-u0E46u0E81u0E82u0E84u0E87u0E88u0E8Au0E8Du0E94-u0E97u0E99-u0E9Fu0EA1-u0EA3u0EA5u0EA7u0EAAu0EABu0EAD-u0EB0u0EB2u0EB3u0EBDu0EC0-u0EC4u0EC6u0EDC-u0EDFu0F00u0F40-u0F47u0F49-u0F6Cu0F88-u0F8Cu1000-u102Au103Fu1050-u1055u105A-u105Du1061u1065u1066u106E-u1070u1075-u1081u108Eu10A0-u10C5u10C7u10CDu10D0-u10FAu10FC-u1248u124A-u124Du1250-u1256u1258u125A-u125Du1260-u1288u128A-u128Du1290-u12B0u12B2-u12B5u12B8-u12BEu12C0u12C2-u12C5u12C8-u12D6u12D8-u1310u1312-u1315u1318-u135Au1380-u138Fu13A0-u13F4u1401-u166Cu166F-u167Fu1681-u169Au16A0-u16EAu16EE-u16F0u1700-u170Cu170E-u1711u1720-u1731u1740-u1751u1760-u176Cu176E-u1770u1780-u17B3u17D7u17DCu1820-u1877u1880-u18A8u18AAu18B0-u18F5u1900-u191Cu1950-u196Du1970-u1974u1980-u19ABu19C1-u19C7u1A00-u1A16u1A20-u1A54u1AA7u1B05-u1B33u1B45-u1B4Bu1B83-u1BA0u1BAEu1BAFu1BBA-u1BE5u1C00-u1C23u1C4D-u1C4Fu1C5A-u1C7Du1CE9-u1CECu1CEE-u1CF1u1CF5u1CF6u1D00-u1DBFu1E00-u1F15u1F18-u1F1Du1F20-u1F45u1F48-u1F4Du1F50-u1F57u1F59u1F5Bu1F5Du1F5F-u1F7Du1F80-u1FB4u1FB6-u1FBCu1FBEu1FC2-u1FC4u1FC6-u1FCCu1FD0-u1FD3u1FD6-u1FDBu1FE0-u1FECu1FF2-u1FF4u1FF6-u1FFCu2071u207Fu2090-u209Cu2102u2107u210A-u2113u2115u2119-u211Du2124u2126u2128u212A-u212Du212F-u2139u213C-u213Fu2145-u2149u214Eu2160-u2188u2C00-u2C2Eu2C30-u2C5Eu2C60-u2CE4u2CEB-u2CEEu2CF2u2CF3u2D00-u2D25u2D27u2D2Du2D30-u2D67u2D6Fu2D80-u2D96u2DA0-u2DA6u2DA8-u2DAEu2DB0-u2DB6u2DB8-u2DBEu2DC0-u2DC6u2DC8-u2DCEu2DD0-u2DD6u2DD8-u2DDEu2E2Fu3005-u3007u3021-u3029u3031-u3035u3038-u303Cu3041-u3096u309D-u309Fu30A1-u30FAu30FC-u30FFu3105-u312Du3131-u318Eu31A0-u31BAu31F0-u31FFu3400-u4DB5u4E00-u9FCCuA000-uA48CuA4D0-uA4FDuA500-uA60CuA610-uA61FuA62AuA62BuA640-uA66EuA67F-uA697uA6A0-uA6EFuA717-uA71FuA722-uA788uA78B-uA78EuA790-uA793uA7A0-uA7AAuA7F8-uA801uA803-uA805uA807-uA80AuA80C-uA822uA840-uA873uA882-uA8B3uA8F2-uA8F7uA8FBuA90A-uA925uA930-uA946uA960-uA97CuA984-uA9B2uA9CFuAA00-uAA28uAA40-uAA42uAA44-uAA4BuAA60-uAA76uAA7AuAA80-uAAAFuAAB1uAAB5uAAB6uAAB9-uAABDuAAC0uAAC2uAADB-uAADDuAAE0-uAAEAuAAF2-uAAF4uAB01-uAB06uAB09-uAB0EuAB11-uAB16uAB20-uAB26uAB28-uAB2EuABC0-uABE2uAC00-uD7A3uD7B0-uD7C6uD7CB-uD7FBuF900-uFA6DuFA70-uFAD9uFB00-uFB06uFB13-uFB17uFB1DuFB1F-uFB28uFB2A-uFB36uFB38-uFB3CuFB3EuFB40uFB41uFB43uFB44uFB46-uFBB1uFBD3-uFD3DuFD50-uFD8FuFD92-uFDC7uFDF0-uFDFBuFE70-uFE74uFE76-uFEFCuFF21-uFF3AuFF41-uFF5AuFF66-uFFBEuFFC2-uFFC7uFFCA-uFFCFuFFD2-uFFD7uFFDA-uFFDC][x240-9A-Zx5Fa-zxAAxB5xBAxC0-xD6xD8-xF6xF8-u02C1u02C6-u02D1u02E0-u02E4u02ECu02EEu0300-u0374u0376u0377u037A-u037Du0386u0388-u038Au038Cu038E-u03A1u03A3-u03F5u03F7-u0481u0483-u0487u048A-u0527u0531-u0556u0559u0561-u0587u0591-u05BDu05BFu05C1u05C2u05C4u05C5u05C7u05D0-u05EAu05F0-u05F2u0610-u061Au0620-u0669u066E-u06D3u06D5-u06DCu06DF-u06E8u06EA-u06FCu06FFu0710-u074Au074D-u07B1u07C0-u07F5u07FAu0800-u082Du0840-u085Bu08A0u08A2-u08ACu08E4-u08FEu0900-u0963u0966-u096Fu0971-u0977u0979-u097Fu0981-u0983u0985-u098Cu098Fu0990u0993-u09A8u09AA-u09B0u09B2u09B6-u09B9u09BC-u09C4u09C7u09C8u09CB-u09CEu09D7u09DCu09DDu09DF-u09E3u09E6-u09F1u0A01-u0A03u0A05-u0A0Au0A0Fu0A10u0A13-u0A28u0A2A-u0A30u0A32u0A33u0A35u0A36u0A38u0A39u0A3Cu0A3E-u0A42u0A47u0A48u0A4B-u0A4Du0A51u0A59-u0A5Cu0A5Eu0A66-u0A75u0A81-u0A83u0A85-u0A8Du0A8F-u0A91u0A93-u0AA8u0AAA-u0AB0u0AB2u0AB3u0AB5-u0AB9u0ABC-u0AC5u0AC7-u0AC9u0ACB-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u0F71-u0F84u0F86-u0F97u0F99-u0FBCu0FC6u1000-u1049u1050-u109Du10A0-u10C5u10C7u10CDu10D0-u10FAu10FC-u1248u124A-u124Du1250-u1256u1258u125A-u125Du1260-u1288u128A-u128Du1290-u12B0u12B2-u12B5u12B8-u12BEu12C0u12C2-u12C5u12C8-u12D6u12D8-u1310u1312-u1315u1318-u135Au135D-u135Fu1380-u138Fu13A0-u13F4u1401-u166Cu166F-u167Fu1681-u169Au16A0-u16EAu16EE-u16F0u1700-u170Cu170E-u1714u1720-u1734u1740-u1753u1760-u176Cu176E-u1770u1772u1773u1780-u17D3u17D7u17DCu17DDu17E0-u17E9u180B-u180Du1810-u1819u1820-u1877u1880-u18AAu18B0-u18F5u1900-u191Cu1920-u192Bu1930-u193Bu1946-u196Du1970-u1974u1980-u19ABu19B0-u19C9u19D0-u19D9u1A00-u1A1Bu1A20-u1A5Eu1A60-u1A7Cu1A7F-u1A89u1A90-u1A99u1AA7u1B00-u1B4Bu1B50-u1B59u1B6B-u1B73u1B80-u1BF3u1C00-u1C37u1C40-u1C49u1C4D-u1C7Du1CD0-u1CD2u1CD4-u1CF6u1D00-u1DE6u1DFC-u1F15u1F18-u1F1Du1F20-u1F45u1F48-u1F4Du1F50-u1F57u1F59u1F5Bu1F5Du1F5F-u1F7Du1F80-u1FB4u1FB6-u1FBCu1FBEu1FC2-u1FC4u1FC6-u1FCCu1FD0-u1FD3u1FD6-u1FDBu1FE0-u1FECu1FF2-u1FF4u1FF6-u1FFCu200Cu200Du203Fu2040u2054u2071u207Fu2090-u209Cu20D0-u20DCu20E1u20E5-u20F0u2102u2107u210A-u2113u2115u2119-u211Du2124u2126u2128u212A-u212Du212F-u2139u213C-u213Fu2145-u2149u214Eu2160-u2188u2C00-u2C2Eu2C30-u2C5Eu2C60-u2CE4u2CEB-u2CF3u2D00-u2D25u2D27u2D2Du2D30-u2D67u2D6Fu2D7F-u2D96u2DA0-u2DA6u2DA8-u2DAEu2DB0-u2DB6u2DB8-u2DBEu2DC0-u2DC6u2DC8-u2DCEu2DD0-u2DD6u2DD8-u2DDEu2DE0-u2DFFu2E2Fu3005-u3007u3021-u302Fu3031-u3035u3038-u303Cu3041-u3096u3099u309Au309D-u309Fu30A1-u30FAu30FC-u30FFu3105-u312Du3131-u318Eu31A0-u31BAu31F0-u31FFu3400-u4DB5u4E00-u9FCCuA000-uA48CuA4D0-uA4FDuA500-uA60CuA610-uA62BuA640-uA66FuA674-uA67DuA67F-uA697uA69F-uA6F1uA717-uA71FuA722-uA788uA78B-uA78EuA790-uA793uA7A0-uA7AAuA7F8-uA827uA840-uA873uA880-uA8C4uA8D0-uA8D9uA8E0-uA8F7uA8FBuA900-uA92DuA930-uA953uA960-uA97CuA980-uA9C0uA9CF-uA9D9uAA00-uAA36uAA40-uAA4DuAA50-uAA59uAA60-uAA76uAA7AuAA7BuAA80-uAAC2uAADB-uAADDuAAE0-uAAEFuAAF2-uAAF6uAB01-uAB06uAB09-uAB0EuAB11-uAB16uAB20-uAB26uAB28-uAB2EuABC0-uABEAuABECuABEDuABF0-uABF9uAC00-uD7A3uD7B0-uD7C6uD7CB-uD7FBuF900-uFA6DuFA70-uFAD9uFB00-uFB06uFB13-uFB17uFB1D-uFB28uFB2A-uFB36uFB38-uFB3CuFB3EuFB40uFB41uFB43uFB44uFB46-uFBB1uFBD3-uFD3DuFD50-uFD8FuFD92-uFDC7uFDF0-uFDFBuFE00-uFE0FuFE20-uFE26uFE33uFE34uFE4D-uFE4FuFE70-uFE74uFE76-uFEFCuFF10-uFF19uFF21-uFF3AuFF3FuFF41-uFF5AuFF66-uFFBEuFFC2-uFFC7uFFCA-uFFCFuFFD2-uFFD7uFFDA-uFFDC]*$/
    
    链接地址: http://www.djcxy.com/p/17002.html

    上一篇: 什么是TypeScript,为什么我会用它来代替JavaScript?

    下一篇: 为什么不是◎ܫ◎和☺有效的JavaScript变量名?