String.fromCharCode()

  • Revision slug: JavaScript/Reference/Global_Objects/String/fromCharCode
  • Revision title: fromCharCode
  • Revision id: 26160
  • Created:
  • Creator: secoif
  • Is current revision? No
  • Comment one or more formatting changes

Revision Content

Summary

Returns a string created by using the specified sequence of Unicode values.

Static Method of String
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 1st Edition

Syntax

String.fromCharCode(num1, ..., numN)

Parameters

num1, ..., numN
A sequence of numbers that are Unicode values.

Description

This method returns a string and not a String object.

Because fromCharCode is a static method of String, you always use it as String.fromCharCode(), rather than as a method of a String object you created.

Getting it to work with higher values

Although most common Unicode values can be represented in a fixed width system/with one number (as expected early on during JavaScript standardization) and fromCharCode() can be used to return a single character for the most common values (i.e., UCS-2 values which are the subset of UTF-16 with the most common characters), in order to deal with ALL legal Unicode values, fromCharCode() alone is inadequate. Since the higher code point characters use two (lower value) "surrogate" numbers to form a single character, fromCharCode() can be used to return such a pair and thus adequately represent these higher valued characters.

Be aware, therefore, that the following utility function to grab the accurate character even for higher value code points, may be returning a value which is rendered as a single character, but which has a string count of two (though usually the count will be one).

// String.fromCharCode() alone cannot get the character at such a high code point
// The following, on the other hand, can return a 4-byte character as well as the 
//   usual 2-byte ones (i.e., it can return a single character which actually has 
//   a string length of 2 instead of 1!)
alert(fixedFromCharCode(0x2F804)); // or 194564 in decimal


function fixedFromCharCode (codePt) {
    if (codePt > 0xFFFF) {
        codePt -= 0x10000;
        return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 +
(codePt & 0x3FF));
    }
    else {
        return String.fromCharCode(codePt);
    }
}

Examples

Example: Using fromCharCode

The following example returns the string "ABC".

String.fromCharCode(65,66,67)

See also

String.prototype.charCodeAt

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/fromCharCode", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/fromCharCode", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/fromCharCode" } ) }}

Revision Source

<h2 name="Summary">Summary</h2>
<p>Returns a string created by using the specified sequence of Unicode values.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Static Method of <a href="/en/JavaScript/Reference/Global_Objects/String" title="en/JavaScript/Reference/Global_Objects/String"><code>String</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.2</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 1st Edition</td> </tr> </tbody>
</table><h2 name="Syntax">Syntax</h2>
<p><code> String.fromCharCode(<em>num1</em>, <em>...</em>, <em>numN</em>) </code></p>
<h2 name="Parameters">Parameters</h2>
<dl> <dt><code>num1, ..., num<em>N</em></code></dt> <dd>A sequence of numbers that are Unicode values.</dd>
</dl>
<h2 name="Description">Description</h2>
<p>This method returns a string and not a <code>String</code> object.</p>
<p>Because <code>fromCharCode</code> is a static method of <code>String</code>, you always use it as <code>String.fromCharCode()</code>, rather than as a method of a <code>String</code> object you created.</p>
<h2>Getting it to work with higher values</h2>
<p>Although most common Unicode values can be represented in a fixed width system/with one number (as expected early on during JavaScript standardization) and <code>fromCharCode()</code> can be used to return a single character for the most common values (i.e., UCS-2 values which are the subset of UTF-16 with the most common characters), in order to deal with ALL legal Unicode values, <code>fromCharCode()</code> alone is inadequate. Since the higher code point characters use two (lower value) "surrogate" numbers to form a single character, <code>fromCharCode()</code> can be used to return such a pair and thus adequately represent these higher valued characters.</p>
<p>Be aware, therefore, that the following utility function to grab the accurate character even for higher value code points, may be returning a value which is rendered as a single character, but which has a string count of two (though usually the count will be one).</p>
<pre class="brush: js">// String.fromCharCode() alone cannot get the character at such a high code point
// The following, on the other hand, can return a 4-byte character as well as the 
//   usual 2-byte ones (i.e., it can return a single character which actually has 
//   a string length of 2 instead of 1!)
<span><span class="string">alert(</span></span>fixedFromCharCode(0x2F804<span><span class="string">));</span></span> // or <span><span class="string">194564</span></span> in decimal


function fixedFromCharCode (codePt) {
    if (codePt &gt; 0xFFFF) {
        codePt -= 0x10000;
        return String.fromCharCode(0xD800 + (codePt &gt;&gt; 10), 0xDC00 +
(codePt &amp; 0x3FF));
    }
    else {
        return String.fromCharCode(codePt);
    }
}
</pre><h2 name="Examples">Examples</h2>
<h3 name="Example:_Using_fromCharCode">Example: Using <code>fromCharCode</code></h3>
<p>The following example returns the string "ABC".</p>
<pre class="brush: js">String.fromCharCode(65,66,67)
</pre>
<h2 name="See_Also">See also</h2>
<p><a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt">String.prototype.charCodeAt</a></p>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/fromCharCode", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/fromCharCode", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/fromCharCode" } ) }}</p>
Revert to this revision