user13964720
user13964720

Reputation:

Why is '\u{1D11E}'.charAt(0) not equal to '\u{1D11E}'?

When I'm trying to evaluate this expression in console I have false as result, why?

console.log('\u{1D11E}'.charAt(0) === '\u{1D11E}')

Upvotes: 3

Views: 120

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597176

'\u{1D11E}' is a string consisting of a single Unicode codepoint U+1D11E. Strings are encoded in UTF-16 format. So each char in the string is a UTF-16 code unit. Thus charAt() returns a code unit, not a codepoint.

U+1D11E is encoded in UTF-16 as 0xD834 0xDD1E, so the string '\u{1D11E}' is actually '\uD834\uDD1E', thus:

'\u{1D11E}'.charAt(0) === '\u{1D11E}' // false
// aka: '\uD834' === '\u{1D11E}'

and

'\u{1D11E}'.charAt(0) === '\uD834' // true
// aka: '\uD834' === '\uD834'

Upvotes: 1

Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

A simple console.log would show you the problem

console.log('\u{1D11E}'.charAt(0))
console.log('\u{1D11E}')
console.log('\u{1D11E}'.charAt(0) === '\u{1D11E}')

As you can see they don't give the same result, that's because charAt only handles UTF-16 code units. See code snippet on same source on how to handle UTF-16 characters (also on other planes, so with code point > 65535).

Upvotes: 3

Related Questions