I’ve done some Google searches, but I get results related to encoding strings or files.
Can I write my Node.js JavaScript source code in UTF-8? Can I use non-ASCII characters in comments, strings, or as variable names?
ECMA-262 seems to require UTF-16 encoding, but Node.js won’t run a UTF-16 encoded .js file. It will, however run UTF-8 source and correctly interpret non-ASCII characters.
So is this by design or by “accident”? Is it specified somewhere that UTF-8 source code is supported?
Yes, you can write your Node.js JavaScript source code in UTF-8 and use non-ASCII characters in comments, strings, or as variable names. In fact, it is recommended to use UTF-8 as the encoding for your source code, as it is more widely supported and is the default encoding for many text editors and IDEs.
According to the ECMA-262 specification, JavaScript source code is expected to be encoded in Unicode, specifically the UTF-16 encoding. However, Node.js allows you to use UTF-8 as the encoding for your source code, and it will correctly interpret non-ASCII characters.
It is not specified in the ECMA-262 specification that UTF-8 source code is supported, but it is supported by Node.js as an extension. This allows you to use non-ASCII characters in your source code, which can be useful for internationalization purposes.
In summary, you can use UTF-8 encoding for your Node.js JavaScript source code and use non-ASCII characters in comments, strings, and variable names. This is supported by Node.js as an extension to the ECMA-262 specification.