How to Get the Unicode of a Character in JavaScript

08/02/2021

Contents

In this article, you will learn how to get the unicode of a character in JavaScript.

Getting the unicode of a character in JavaScript

Unicode is a character encoding standard that assigns unique numbers to each character. In JavaScript, you can get the Unicode of a character using built-in functions.

Using the charCodeAt() method

The first method is using the charCodeAt() method to get the Unicode of a character. Here is an example code snippet:

const str = "A";
const unicode = str.charCodeAt(0);
console.log(unicode); // Output: 65

Explanation

  • str.charCodeAt(0) gets the Unicode of the character at index 0 in the string.
  • The Unicode value of “A” is 65.

Using the codePointAt() method

The second method is using the codePointAt() method to get the Unicode of a character. Here is an example code snippet:

const str = "😊";
const unicode = str.codePointAt(0);
console.log(unicode); // Output: 128522

Explanation

  • str.codePointAt(0) gets the Unicode of the character at index 0 in the string.
  • The Unicode value of the emoji “😊” is 128522.

Using the unicode escape sequence

The third method is using the Unicode escape sequence to get the Unicode of a character. Here is an example code snippet:

const str = "🌟";
const unicode = "\u{1F31F}".codePointAt(0);
console.log(unicode); // Output: 127775

Explanation

  • “\u{1F31F}” is the Unicode escape sequence for the star emoji “🌟”.
  • “\u{1F31F}”.codePointAt(0) gets the Unicode of the character represented by the escape sequence at index 0 in the string.
  • The Unicode value of the star emoji “🌟” is 127775.