JavaScript String fromCharCode() Method
The fromCharCode()
method in JavaScript is a static method of the String
object. Which is used to create a string from a sequence of Unicode values.
Syntax:
String.fromCharCode(n1, n2, ..., nX)
Parameters:
The method takes the UTF-16 Unicode sequences as its argument. The number of arguments to this method depends upon the number of characters to be joined as a string. The range of the numbers is between 0 and 65535.
Return value:
The return value of this method is a string containing the characters whose UTF-16 codes were passed to the method as arguments.
Example 1: Using JavaScript’s String.fromCharCode() Method to Generate a String
The function func()
uses String.fromCharCode()
to convert Unicode values (71, 70, 71) into characters (‘G’, ‘F’, ‘G’), generating the string “GFG” which is then logged to the console.
function func() {
let str = String.fromCharCode(71, 70, 71);
console.log(str);
}
func();
Output
GFG
Example 2: Converting Unicode Point to Character with JavaScript’s fromCharCode()
The function func()
utilizes String.fromCharCode()
to translate the UTF-16 code point 0x12014
into its respective character. Subsequently, the character is printed to the console, showcasing the conversion process.
// JavaScript to illustrate fromCharCode() method
function func() {
// UTF-16 code to be converted into character
let str = String.fromCharCode(0x12014);
console.log(str);
}
func();
Output
—
Supported Browsers:
- Google Chrome 85
- Microsoft Edge 85
- Mozilla Firefox 77
- Opera 71
- Safari 13.1