Home >Web Front-end >HTML Tutorial >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 value is the standard value of a character and can be encoded by the user to convert it into a character.
For example, "A" is a Unicode character whose value is 65 according to the ASCII (American Standard Code for Information Interchange) table. Likewise, all letters, numbers, and other characters have specific Unicode values. We will learn to use JavaScript to identify Unicode characters from their values.
In JavaScript, the string library contains the fromCharCode() method, which gets the decimal value of a Unicode character and returns the associated character. Additionally, it takes multiple values as arguments and returns a string after combining all characters.
We can use the fromCharCode() method to convert Unicode values to characters according to the following syntax.
String.fromCharCode( value ); String.fromCharCode( v1, v2, v3, v4, v5, … );
value - It is the decimal value of the Unicode character.
v1, v2, v3, ... - They are multiple values of different or the same Unicode value.
In the example below, we use the fromCharCode() method on different Unicode values to convert them to characters. We convert two Unicode values - 69 and 97 into 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>
In the following program, we use the fromCharCode() method to convert multiple Unicode values to characters. We convert two sets of Unicode values to char strings, the 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 multiple values of Unicode characters to the fromCharCode() method, it will output a string of related Unicode characters.
Users have learned and understood how the fromCharCode() method converts Unicode values to Unicode characters. Users can use the fromCharCode() method whether it is a single value or multiple values separated by commas.
The above is the detailed content of How to convert Unicode values to characters in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!