TIL why emojis in JS counts as a length of 2
POSTED ON:
TAGS: javascript fonts
You can put emojis in code.
Take a look at this:
const smile = 'đ';
smile.length; // => 2
Why is it a length
of 2?
Itâs because JavaScript considers strings as a sequence of code units, rather than a sequence of visible characters
The Javascript Specs:
The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values (âelementsâ). The String type is generally used to represent textual data in a running ECMAScript program, in which case each element in the String is treated as a UTF-16 code unit value.
Look at that word code unit
.
That means every symbol/character is a code unit from 0x0000
until 0xFFFF
.
Another key word is UTF-16s. We're talk about that later.
const letter = '\u0048';
letter === 'H' // => true
The grinning face character 'đ', which would have the code unit of 0x1F600
(the number 0x1F600
is bigger than 0xFFFF
thus doesnât fit into 16 bits), is encoded with a sequence of 2 code units 0xD83D0xDE00
.
Via
https://dmitripavlutin.com/what-is-string-in-javascript/
Related TILs
Tagged: javascript