Languages
[Edit]
EN

JavaScript - strings default encoding

6 points
Created by:
Geospatial-Palus
660

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:

  1. JavaScript - base64 with Unicode support
  2. JavaScript - convert string to bytes array (UTF-8)

  3. JavaScript - convert bytes array to string (UTF-8)

 

References

  1. String value - ECMAScript® 2015 Language Specification

Alternative titles

  1. JavaScript - text default encoding
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join