Wednesday, 29 July 2015

JavaScript Variable Names

In schappo.blogspot.co.uk/2015/06/php-variable-names.html I explained how PHP variable names are determined at the byte level. JavaScript variable names are determined at a higher level and are defined in terms of Unicode Properties and General Categories.

Letʼs start with some simple Basic Latin variable name examples.
  • Valid Names:  nottingham  nottingham8
  • Invalid Name:  8nottingham
where 8 is Unicode character U+0038 DIGIT 8. The last name, above, being invalid because it begins with a digit.

We are in the Unicode age and so do not need to restrict ourselves to Basic Latin. Some time ago I asked myself whether the same Basic Latin rule, an initial digit is invalid, applies to other Scripts and yes it does (well mostly).
  • Valid Devanagari Names:  नाटिंघम  नाटिंघम६
  • Invalid Devanagari Name:  ६नाटिंघम
where ६ is Unicode character U+096C DEVANAGARI DIGIT SIX
  • Valid Thai Names:  นอตทิงแฮม  นอตทิงแฮม๘
  • Invalid Thai Name:  ๘นอตทิงแฮม
where ๘ is Unicode character U+0E58 THAI DIGIT EIGHT
  • Valid Telugu Names:   నాటింగ్‌హామ్  నాటింగ్‌హామ్౬
  • Invalid Telugu Name:  ౬నాటింగ్‌హామ్
where ౬ is Unicode character U+0C6C TELUGU DIGIT SIX
  • Valid Chinese Names:  诺丁汉  诺丁汉八  八诺丁汉
where 八 is the digit 8, aka Unicode character U+516B CJK UNIFIED IDEOGRAPH-516B

So, Unicode CJK Ideographs are the exception as a name can begin with a digit. Unicode CJK Ideographs encompass Chinese Hanzi, Japanese Kanji and Korean Hanja. The reason for this exception is because of the Unicode General Category to which these digits are assigned. The digits 8, ६, ๘, ౬ have the General Category Nd which is Decimal Number and thus, cannot be the first character of a variable name. 八 has the General Category Lo which is Other Letter and thus, can be the first character of a variable name even though semantically it is a digit.

The above holds true for ECMAScript 5 and 6 and possibly earlier versions but it is unlikely I will test earlier versions. You can validate your variable names online at mothereff.in/js-variables.

For further reading I suggest: