Open In App

How to Insert Unicode in JavaScript ?

Last Updated : 10 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Unicode is a computing standard for consistent encoding, representation, and handling of text. Unicode characters are necessary when making multilingual applications or working with special symbols and emojis in JavaScript. It provides unique numbers for each character. UTF-8 is a popular Unicode encoding standard that has 8-bit units.

The below approaches can be used to insert Unicode in JavaScript.

Using the Unicode number values

You can use the Unicode number values for different kinds of symbols or characters by using the \u escape sequence before each hexadecimal value.

Syntax:

\uXXXX

Here, XXXX stands for different hexadecimal values for different symbols.

Example: The below code will explain the use of the Unicode number values to add Unicode characters in JavaScript.

JavaScript
const copyRight = '\u00A9';
const hash = '\u0023';
console.log(copyRight);
console.log(hash);

Output
©
#

Using the String.fromCodePoint() method

JavaScript provides a String.fromCodePoint() method that creates a string from a sequence of unicode code points. This method takes one or more arguments as input and return their corresponding characters.

Syntax:

String.fromCodePoint(codePoint1, codePoint2, ...)

Example: The below code will implement the String.fromCodePoint() method to insert the unicode character.

JavaScript
const smile = 0x1F60A;
const heart = 0x2764;
console.log(String.
    fromCodePoint(smile));
console.log(String.
    fromCodePoint(heart));

Output
😊
❤

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads