JS: String.prototype.charCodeAt (Char to ID)
str.charCodeAt(index)-
- Return a integer that's the Code Point at position index of str.
- Return NaN when out of bound.
// character: a (codepoint 97, #x61) console.assert( "abc".charCodeAt(0) === 97, ); // character: b (codepoint 98, #x62) console.assert( "abc".charCodeAt(1) === 98, ); // character: α (codepoint 945, #x3b1) console.assert( "α".charCodeAt(0) === 945, ); // s------------------------------ // return NaN when out of bound console.assert( Number.isNaN("".charCodeAt(0)), ); 🛑 warning: if string contains a character outside of Basic Multilingual Plane (such as most emoji 🦋 or rarely used Chinese character), result of string methods may be unexpected. See JS: String Index and Code Unit.
console.assert( "🦋".charCodeAt(0) === 55358, ); console.assert( "🦋".charCodeAt(0).toString(16) === "d83e", ); // d83e is the first part of surrogate pair in utf 16 encoding for the butterfly char /* 🦋 Name: BUTTERFLY Codepoint: 129419 HEXD: 1F98B UTF8: F0 9F A6 8B UTF16: D83E DD8B */ // get the second part of surrogate pair console.assert( "🦋".charCodeAt(1).toString(16) === "dd8b", );