How to Convert Unicode Values to Characters in JavaScript

08/02/2021
Contents
In this article, you will learn how to convert unicode values to characters in JavaScript.
Converting unicode values to characters in JavaScript
Unicode is a character encoding standard that assigns unique numbers to each character. In JavaScript, you can convert Unicode values to characters using built-in functions.
Using the fromCharCode() method
The first method is using the fromCharCode() method to convert a Unicode value to a character. Here is an example code snippet:
const unicode = 65;
const char = String.fromCharCode(unicode);
console.log(char); // Output: "A"
Explanation
- String.fromCharCode(unicode) converts the Unicode value to its corresponding character.
- The Unicode value 65 represents the character “A”.
Using the String.fromCodePoint() method
The second method is using the fromCodePoint() method to convert a Unicode value to a character. Here is an example code snippet:
const unicode = 128522;
const char = String.fromCodePoint(unicode);
console.log(char); // Output: "π"
Explanation
- String.fromCodePoint(unicode) converts the Unicode value to its corresponding character.
- The Unicode value 128522 represents the emoji “π”.
Using the Unicode escape sequence
The third method is using the Unicode escape sequence to convert a Unicode value to a character. Here is an example code snippet:
const unicode = 127775;
const char = "\u{1F31F}";
console.log(char); // Output: "π"
Explanation
- “\u{1F31F}” is the Unicode escape sequence for the star emoji “π”.
- The Unicode value 127775 represents the star emoji “π”.