1

ECMAScript treats strings as UTF-16.

If I write a program in my text editor, I assume the most-likely default encoding will be UTF-8.

console.log('🌿')

So how does this "work"? Does it work because UTF-16 is directly compatible with UTF-8, which, in turn, is directly compatible with ASCII?

1 Answer 1

2

See section 10.1, Source Text:

ECMAScript code is expressed using Unicode. ECMAScript source text is a sequence of code points. All Unicode code point values from U+0000 to U+10FFFF, including surrogate code points, may occur in source text where permitted by the ECMAScript grammars. The actual encodings used to store and interchange ECMAScript source text is not relevant to this specification. Regardless of the external source text encoding, a conforming ECMAScript implementation processes the source text as if it was an equivalent sequence of SourceCharacter values, each SourceCharacter being a Unicode code point.

Sign up to request clarification or add additional context in comments.

2 Comments

So JavaScript engines typically have within them a text encoding detection capability, that permits either conversion of source text to UTF-16, or the treatment of differently encoded source text as UTF-16?
@52d6c6af JS engines just have an unicode sequence input, they don't have any encoding detection. That's part of the embedding, e.g. the HTTP code in the browser and for Node.js I actually have no idea. The unicode sequence is fed to the parser, which constructs String values from literals (which includes things like converting escape sequences).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.