TransWikia.com

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

Stack Overflow Asked by Json Prime on November 20, 2021

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

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

2 Answers

'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 'uD834uDD1E', thus:

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

and

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

Answered by Remy Lebeau on November 20, 2021

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).

Answered by Alexandre Elshobokshy on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP