Skip to content

How to insert Unicode in JavaScript

A code point (also known as “character code”) is a numerical representation of a specific Unicode character. In JavaScript, String#charCodeAt() can be used to get the numeric Unicode code point of any character up to U+FFFF (i.e. the character with code point 0xFFFF , which is 65535 in decimal).

The Unicode Standard provides a unique number for every character, no matter the platform, device, application, or language. UTF-8 is a popular choice for Unicode encoding which has 8-bit code units.

In Javascript, string literals can be written in Unicode using Unicode escape sequence (). The general syntax is \uXXXX, where X denotes four hexadecimal digits. For example, the letter N is denoted as ‘\u004E’ in Unicode. To write the word “NO” in Unicode, we can use the following code.

let value = '\u004E\u004F';
console.log(value); //NO

All Unicode characters can be represented in this way. To represent a HEART emoji, the Unicode characters would be like:

let value = '\u200D\u2764\uFE0F\u200D';
console.log(value); //❤️ <-- Heart is supposed to be printed here but looks like this code plugin doesn't support Emoji. My bad.

When you want to express an identifier itself in Unicode, yeah why not?

let \u004E\u004F = "value";
console.log(NO); //value

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.