JavaScript fromCharCode() Method
This tutorial introduces the fromCharCode() method, from the JavaScript String object. The fromCharCode() method allows you to conver decimal Unicode values into actual characters; the method takes one or more integers as arguments, and returns the string of concatenated (put together) characters corresponding to the integers supplied.
Related string method:
The String.charCodeAt() method returns the decimal Unicode value for the character at the given position index passed as argument.
Using the fromCharCode() Method
The fromCharCode() method is applied directly on the JavaScript String object, as shown below. The method takes at least one argument, and optionally several arguments separated by commas.
From our charCodeAt() tutorial, we remember that the decimal Unicode values 74 and 106 correspond to, respectively, the uppercase and lowercase letter "J".
alert( String.fromCharCode(74, 106) );
Our single line of JavaScript code above calls the fromCharCode() method by passing it two arguments (spaces between numerical arguments and commas are ignored by the JavaScript interpreter). As expected, our result is:
Determining Decimal Unicode Values
It is easy to find out which decimal Unicode value a certain character has, by calling the charCodeAt() method on a single-character string:
alert( "A".charCodeAt(0) );
This second one-line script produces the following result; you can live-test it yourself at the bottom of this page.
Test the fromCharCode() Method
Interactively test the fromCharCode() method by editing the JavaScript code below and clicking the Test fromCharCode() Method button.
Browser support for JavaScript fromCharCode() method | |||
---|---|---|---|