This JavaScript tutorial explains how to use the string method known as fromCharCode() with syntax and examples.
Description
In JavaScript, fromCharCode() is a string method that is used to create a string from a sequence of Unicode values. Because the fromCharCode() method is a static technique of the String constructor, it should be invoked via the String constructor object rather than via the precise occasion of the String class.
Syntax
In JavaScript, the syntax for the fromCharCode() technique is:
String.fromCharCode(value1, value2, ... value_n);
Parameters or Arguments
value1, value2, … value_n The Unicode values to convert to characters and then concatenate collectively into a string.
Returns
The fromCharCode() method accepts a sequence of Unicode values. These Unicode values are UTF-16 values (16-bit integers between zero and 65535) that are converted to characters and concatenated together in a string. This ensuing string is then returned via the fromCharCode() method.
Note
The fromCharCode() technique does not exchange the fee of the unique string. Use the fromCodePoint() method if any of the Unicode values you want to convert are not representable in a single UTF-16 code unit.
Example
Let’s take a seem at an instance of how to use the fromCharCode() technique in JavaScript.
For example:
console.log(String.fromCharCode(84,101,99,104,79,110,84,104,101,78,101,116));
In this example, we have invoked the fromCharCode() static method through the String constructor object. This technique will convert the 12 Unicode values to characters and then concatenate them into a single string result.
We have written the output of the fromCharCode() method to the net browser console log, for demonstration purposes, to show what the fromCharCode() method returns.
The following will be output to the net browser console log:
TechOnTheNet
As you can see, the fromCharCode() method converted the 12 Unicode values into their corresponding characters, concatenated them collectively into a single string, and then lower back the ensuing string.
84 = ‘T’ 101 = ‘e’ 99 = ‘c’ 104 = ‘h’ 79 = ‘O’ 110 = ‘n’ 84 = ‘T’ 104 = ‘h’ 101 = ‘e’ 78 = ‘N’ 101 = ‘e’ 116 = ‘t’
This makes the resulting string ‘TechOnTheNet’.
Leave a Review