Ben Aston
Ben Aston

Reputation: 55729

How does string encoding work in JavaScript program text?

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?

Upvotes: 1

Views: 191

Answers (1)

Bergi
Bergi

Reputation: 664528

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.

Upvotes: 2

Related Questions