JavaScript - strings default encoding
In this short article, we would like to answer for the question: What encoding uses JavaScript to store strings?
Quick answer:
Modern JavaScript engines store strings using UTF-16 encoding format (they uses surrogate pairs for some characters, e.g. ❤️).
Where: Modern JavaScript engines means: >= ECMAScript 2015 / ES6.
From specification:
String value
is primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer.
NOTE:
A String value is a member of the String type. Each integer value in the sequence usually represents a single 16-bit unit of UTF-16 text. However, ECMAScript does not place any restrictions or requirements on the values except that they must be 16-bit unsigned integers.
(source available is here)
In details
UTF-16 can encode Unicode characters using 1 or 2 code units.
When we use Ajax / fetch()
API, etc., engine makes conversion automitically for us from UTF-16 to other encoding (and back).
Custom cases
Although sometimes we need to take care of encoding by self - it is really rare case.
According to sense of conversion between string and UTF-8, the exampel solutions are avaialble here: