JavaScript: What Are Allowed Characters in Identifiers (Variable & Function Names)? Unicode?

, , …,
Want to master JavaScript in a month? Commit. Buy Xah JavaScript Tutorial. You also get Xah HTML Tutorial and Xah CSS Tutorial.

What's allowed characters in JavaScript identifiers (function & variable names)?

JavaScript identifiers must:

ECMAScript §7#sec-7.6

Here's a example you can test:

// -*- coding: utf-8 -*-
// α GREEK SMALL LETTER ALPHA codepoint 945, x3b1
var α = 4;                      // ok
// -*- coding: utf-8 -*-
// ♥ BLACK HEART SUIT codepoint 9829, #x2665
// is not a letter
var ♥ = 3; // SyntaxError: Unexpected token ILLEGAL
// -*- coding: utf-8 -*-
// 𓀀, EGYPTIAN HIEROGLYPH A001, x13000, (is a letter, but Not a Basic Multi-Lingual Plane)
var x𓀀 = 3; // SyntaxError: Unexpected token ILLEGAL

More example to play with:

// -*- coding: utf-8 -*-
// test If unicode is supported in var name or function name.

// Chinese char “愛” (x611b) in BMP, and is a Unicode letter
var 愛 = "♥";

// lambda λ (x3bb) in BMP, and is a Unicode letter
function λ(x) {return x;}

console.log(λ(愛));  // prints ♥

(To search for Unicode, or find a character's code point, see: Unicode Characters ☢ ☯ ☭ ∑ ∞ ♀ ♂ ♥.)

What characters are Unicode Letter?

A “Unicode Letter” is a character whose Unicode property “general category”'s major value is “L”. In general, language's alphabets are Unicode letters, and Chinese characters are also Unicode Letter.

Examples of characters that's not Unicode letter: digits, punctuations, math symbols, emoticons.

How to determine if a character is a Unicode Letter?

You can find out whether a Unicode char is a letter using python: Python: Processing Unicode: unicodedata Module Tutorial, or use emacs's “describe-char” command: Emacs & Unicode Tips.

Case Sensitivity in {HTML5, XML, CSS, JavaScript}

Case Sensitivity in {HTML5, XML, CSS, JavaScript}

blog comments powered by Disqus