Emojis and other special characters take length 2
xxxxxxxxxx
const emoji = ""; // Emojis not supported in codegrepper-answers. Just insert any emoji here
console.log(emoji.length) // 2
console.log(Array.from(emoji).length) // 1
Explanation:
"In UTF-16 (the encoding system used for JavaScript strings) code units are 16-bit values. [..] .Not all of the code points defined by Unicode fit into 16 bits, many Unicode code points are encoded as a pair of UTF-16 code units, which is called a surrogate pair."
xxxxxxxxxx
/* The length property of a String object contains the length of the
string, in UTF-16 code units. length is a read-only data property of
string instances. */
const str = "Life, the universe and everything. Answer:";
console.log(`${str} ${str.length}`);
// expected output: "Life, the universe and everything. Answer: 42"