Unicode is a standard for representing and managing text in most of the world’s writing systems. Virtually all modern software that works with text, supports Unicode. The standard is maintained by the Unicode Consortium. A new version of the standard is published every year (with new Emojis etc.). Unicode 1 was published in 1991.
Two concepts are crucial for understanding Unicode:
The first version of Unicode had 16-bit code points. Since then, the number of characters has grown considerably and the size of code points was extended to 21 bits. These 21 bits are partitioned in 17 planes, with 16 bits each:
Planes 1-16 are called supplementary planes or astral planes.
Let’s check the code points of a few characters:
The hexadecimal number of the code points tells us that the first three characters reside in plane 0 (within 16 bits), while the emoji resides in plane 1.
Let’s cover three ways of encoding code points as code units.
UTF-32 uses 32 bits to store code units, resulting in one code unit per code point. This format is the only one with fixed-length encoding (all others use a varying number of code units to encode a single code point).
UTF-16 uses 16-bit code units. It encodes code points as follows:
BMP (first 16 bits of Unicode): are stored in single code units.
Astral planes: After subtracting the BMP’s count of 0x10000 characters from Unicode’s count of 0x110000 characters, 0x100000 characters (20 bits) remain. These are stored in unoccupied “holes” in the BMP:
As a consequence, by looking at a UTF-16 code unit, we can tell if it is a BMP character, the first part (leading surrogate) of an astral plane character or the last part (trailing surrogate) of an astral plane character.
UTF-8 has 8-bit code units. It uses 1–4 code units to encode a code point:
|Code points||Code units|
|0000–007F||0xxxxxxx (7 bits)|
|0080–07FF||110xxxxx, 10xxxxxx (5+6 bits)|
|0800–FFFF||1110xxxx, 10xxxxxx, 10xxxxxx (4+6+6 bits)|
|10000–1FFFFF||11110xxx, 10xxxxxx, 10xxxxxx, 10xxxxxx (3+6+6+6 bits)|
For web development, two Unicode encoding formats are relevant: UTF-16 and UTF-8.
The ECMAScript specification internally represents source code as UTF-16.
For more information on Unicode and strings, consult the section on atoms of text in the chapter on strings.
.js files, the encoding is almost always UTF-8, these days:
The concept of a character becomes remarkably complex, once you consider many of the world’s writing systems.
On one hand, code points can be said to represent Unicode “characters”.
On the other hand, there are grapheme clusters. A grapheme cluster corresponds most closely to a symbol displayed on screen or paper. It is defined as “a horizontally segmentable unit of text”. One or more code points are needed to encode a grapheme cluster.
For example, one emoji of a family is composed of 7 code points – 4 of them are graphemes themselves and they are joined by invisible code points:
Another example is flag emojis:
Reading: More information on grapheme clusters
For more information, consult “Let’s Stop Ascribing Meaning to Code Points” by Manish Goregaokar.
See quiz app.