How to convert Unicode values to characters in JavaScript?


In this tutorial, we will learn to convert Unicode values to characters in JavaScript. The Unicode values are the standard values for the character, and users can encode them to convert them into characters.

For example, ‘A’ is a Unicode character whose value is 65 according to the ASCII (American standard code for information interchange) table. In the same way, all alphabets, numbers, and other characters have particular Unicode values. We will learn to identify the Unicode character from its values using JavaScript.

Using the fromCharCode() Method

In JavaScript, the string library contains the fromCharCode() method, which takes the decimal values of the Unicode characters and returns related characters. Also, it takes the multiple values as a parameter and returns the string after combining all the characters.

Syntax

We can follow the syntax below to convert the Unicode values to characters using the fromCharCode() method.

String.fromCharCode( value );
String.fromCharCode( v1, v2, v3, v4, v5, … );

Parameter

  • value − It is the decimal value of a Unicode character.

  • v1, v2, v3, … − They are multiple values for the different or same Unicode values.

Example 1

In the example below, we have used the fromCharCode() method for different Unicode values to convert them into characters. We convert two Unicode values- 69 and 97 to characters.

<html> <head> </head> <body> <h2>Convert Unicode values to characters in JavaScript.</h2> <h4>Converting single Decimal Unicode value to character using the <i> fromCharCode() </i> method.</h4> <p id = "output"> </p> <script> let output = document.getElementById("output"); // converting different decimal values to characters let value = 69; let char = String.fromCharCode( value ); output.innerHTML += value + " to unicode character is : " + char + " <br/> "; char = String.fromCharCode( 97 ); output.innerHTML += 97 + " to unicode character is : " + char + " <br/> "; </script> </body> </html>

Example 2

In the program below, we use the fromCharCode() method to convert multiple Unicode values to characters. We convert two sets of Unicode values to char strings, first to "TutorialsPoint" and the second to "Hello".

<html> <head> </head> <body> <h2>Convert Unicode values to characters in JavaScript. </h2> <h4>Converting multiple Decimal Unicode values to characters using the <i> fromCharCode() </i> method.</h4> <p id="output1"></p> <script> let output1 = document.getElementById("output1"); // converting multiple values to unicode characters in single pass. output1.innerHTML += " [84, 117, 116, 111, 114, 105, 97, 108, 115, 80, 111, 105, 110, 116] to Unicode character is : " + String.fromCharCode( 84, 117, 116, 111, 114, 105, 97, 108, 115, 80, 111, 105, 110, 116 ) + " <br/> "; output1.innerHTML += " [72, 69, 76, 76, 79] to Unicode character is : " + String.fromCharCode( 72, 69, 76, 76, 79 ) + " <br/> "; </script> </body> </html>

In the above output, users can see that when we pass the multiple values of Unicode character to fromCharCode() method, it outputs string of related Unicode characters.

Users have learned and understood how the fromCharCode() method works to convert the Unicode values to Unicode characters. Users can use the fromCharCode() method, either for the single value or multiple values with commas separated.

Updated on: 22-Aug-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements