Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 object contains the fromCharCode() method, which takes the decimal values of the Unicode characters and returns related characters. Also, it takes multiple values as parameters 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, ...);
Parameters
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: Converting Single Unicode Values
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>
69 to unicode character is : E 97 to unicode character is : a
Example 2: Converting Multiple Unicode Values
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>
[84, 117, 116, 111, 114, 105, 97, 108, 115, 80, 111, 105, 110, 116] to Unicode character is : TutorialsPoint [72, 69, 76, 76, 79] to Unicode character is : HELLO
Example 3: Using Array with fromCharCode()
You can also use the spread operator to convert an array of Unicode values:
<html>
<head>
</head>
<body>
<h4>Converting array of Unicode values using spread operator.</h4>
<p id="output2"></p>
<script>
let output2 = document.getElementById("output2");
let unicodeArray = [87, 111, 114, 108, 100]; // "World"
let result = String.fromCharCode(...unicodeArray);
output2.innerHTML = "Unicode array [87, 111, 114, 108, 100] converts to: " + result;
</script>
</body>
</html>
Unicode array [87, 111, 114, 108, 100] converts to: World
Key Points
The
fromCharCode()method is a static method of the String object.It accepts decimal Unicode values (0-65535 for Basic Multilingual Plane).
Multiple values can be passed as separate parameters to create strings.
For Unicode code points above 65535, use
String.fromCodePoint()instead.
Conclusion
The String.fromCharCode() method provides a simple way to convert Unicode decimal values to their corresponding characters. It works with single values or multiple values to create complete strings from Unicode sequences.
