The static String.fromCharCode()
method returns a string created from the specified sequence of UTF-16 code units.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
String.fromCharCode(num1[, ...[, numN]])
Parameters
num1, ..., numN
- A sequence of numbers that are UTF-16 code units. The range is between 0 and 65535 (0xFFFF). Numbers greater than 0xFFFF are truncated. No validity checks are performed.
Return value
A string of length N consisting of the N specified UTF-16 code units.
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.
Examples
Using fromCharCode()
The following example returns the string "ABC".
String.fromCharCode(65, 66, 67); // returns "ABC" String.fromCharCode(0x2014) // returns "—" String.fromCharCode(0x12014) // also returns "—"; the digit 1 is truncated and ignored
Getting it to work with higher values
Although most common Unicode values can be represented with one 16-bit 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 (up to 21 bits), fromCharCode()
alone is inadequate. Since the higher code point characters use two (lower value) "surrogate" numbers to form a single character, String.fromCodePoint()
(part of the ES2015 standard) can be used to return such a pair and thus adequately represent these higher valued characters.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262) The definition of 'StringfromCharCode' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.fromCharCode' in that specification. |
Standard | |
ECMAScript Latest Draft (ECMA-262) The definition of 'String.fromCharCode' in that specification. |
Draft |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | Node.js | |
Basic support | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
Legend
- Full support
- Full support