JS: Convert Character To/From Codepoint

By Xah Lee. Date: . Last updated: .

Convert a Character to Codepoint

Use string.codePointAt (index) to get the codepoint of character at index in string

[see JS: String.prototype.codePointAt]

[see JS: String.prototype.charCodeAt]

// get a Unicode char's codepoint

console.log(
    "α".charCodeAt(0)
); // prints 945, correct

console.log(
    "😂".charCodeAt(0)
); // prints 55357, WRONG!

console.log(
    "😂".codePointAt(0)
); // prints 128514, correct

// α
// Name: GREEK SMALL LETTER ALPHA
// codepoint decimal: 945
// codepoint hexadecimal: 3b1

// 😂
// name: FACE WITH TEARS OF JOY
// codepoint decimal: 128514
// codepoint hexadecimal: 1f602

[see JS: Convert Decimal/Hexadecimal]

[see JS: String Code Unit vs Code Point]

Convert Unicode Codepoint to Character

Use String.fromCodePoint(n1, n2 …) to create a string made of codepoint n1, n2, etc.

[see JS: String.fromCodePoint]

[see JS: String.fromCharCode]

// create string from unicode code points

// correct
console.log(
    String.fromCodePoint(945, 128514) === "α😂"
); // true

// incorrect for second char
console.log(
    String.fromCharCode(945, 128568)
); // returns "α"

[see JS: String Code Unit vs Code Point]

Character Topic

  1. JS: String Code Unit vs Code Point
  2. JS: Convert Character To/From Codepoint
  3. JS: String.fromCodePoint
  4. JS: String.fromCharCode
  5. JS: String.prototype.charAt
  6. JS: String.prototype.charCodeAt
  7. JS: String.prototype.codePointAt
  8. JS: Convert Decimal/Hexadecimal
  9. JS: Unicode Character Escape Sequence

String Topic

  1. JS: Default Charset/Encoding
  2. JS: String Code Unit vs Code Point
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

  1. JS: Template String
  2. JS: Convert String to Number
  3. JS: Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON Object

  1. JS: String Object
  2. JS: String.prototype
  3. JS: String Constructor
Liket it? Put $1 at patreon.

Or, Buy JavaScript in Depth