JavaScript fromCharCode() to Build a String from UTF-16 Code Units
Use String.fromCharCode(72, 73) to turn UTF-16 code-unit numbers into a JavaScript string. This example creates HI. If you have one Unicode code point such as U+1F642, use String.fromCodePoint(0x1F642).
Convert numbers to UTF-16 code units
fromCharCode() is a static method on String. Each argument contributes one 16-bit code unit in the same order, and a call with no arguments returns the empty string. Use charCodeAt() to read a code unit back as a number. ECMAScript · String.fromCharCode()
console.log(String.fromCharCode(72)); // 'H'
console.log(String.fromCharCode(72, 69, 76, 76, 79)); // 'HELLO'
console.log('H'.charCodeAt(0)); // 72
console.log(String.fromCharCode(72, 73)); // 'HI'
console.log(String.fromCharCode()); // ''
Distinguish code units from code points
A Unicode code point is a number assigned in Unicode. A UTF-16 code unit is one 16-bit value used to represent text. For example, U+1F642 uses two units, 0xD83D and 0xDE42. In that order, they form a surrogate pair.
const point = 0x1F642;
const face = String.fromCodePoint(point);
console.log(face.length); // 2
console.log(face.charCodeAt(0)); // 55357
console.log(face.charCodeAt(1)); // 56898
console.log(String.fromCharCode(55357, 56898) === face); // true
console.log(String.fromCharCode(point) === face); // false
console.log(String.fromCharCode(0xD83D, 0xDE42) === face); // true
If you already have both units, pass both to fromCharCode(). Passing a high or low surrogate on its own leaves an unpaired code unit. If you have the code point instead, fromCodePoint() supplies the required UTF-16 units. It accepts integers from 0 through 0x10FFFF. ECMAScript · String.fromCodePoint()
Validate numbers before conversion
For ordinary numbers, fromCharCode() drops the fractional part and wraps into the 16-bit range. For example, 65536 becomes 0, -1 becomes 65535, and 72.9 becomes 72. To require a code-unit value, check that it is an integer from 0 through 65535 before conversion.
console.log(String.fromCharCode(65536).charCodeAt(0)); // 0
console.log(String.fromCharCode(-1).charCodeAt(0)); // 65535
console.log(String.fromCharCode(72.9).charCodeAt(0)); // 72
function codeUnitToText(value) {
if (!Number.isInteger(value) || value < 0 || value > 65535) {
throw new RangeError("Expected a UTF-16 code unit.");
}
return String.fromCharCode(value);
}
console.log(codeUnitToText(72)); // 'H'
For text input, decide which numeric forms to accept. The demo below reads decimal integers and hexadecimal values starting with 0x. It rejects blanks, fractions, negatives, and out-of-range values. Checking the range of each number leaves surrogate pairing as a separate check.
Build a string and read its code units back
Save the code as string-from-char-code.html in UTF-8 and open it in a browser. The default list produces HELLO. Enter 65, 66 and select the button to see AB and the code-unit values 65, 66 read back from the string.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Build a string from UTF-16 code units</title>
</head>
<body>
<label for="units">UTF-16 code units (comma-separated)</label>
<input id="units" type="text" value="72, 69, 76, 76, 79">
<button id="convert" type="button">Build string</button>
<pre id="output" aria-live="polite"></pre>
<script>
const units = document.querySelector('#units');
const output = document.querySelector('#output');
function convert() {
const parts = units.value.split(',').map(part => part.trim());
const values = parts.map(part => Number(part));
if (parts.some(part => !/^(?:[0-9]+|0x[0-9a-f]+)$/i.test(part)) ||
values.some(value => !Number.isInteger(value) || value < 0 || value > 65535)) {
output.textContent = "Enter comma-separated decimal or 0x hexadecimal integers from 0 to 65535.";
return;
}
const text = values.map(value => String.fromCharCode(value)).join('');
output.textContent = `String: ${JSON.stringify(text)}\n` +
`UTF-16 values read back: ${Array.from({ length: text.length },
(_, index) => text.charCodeAt(index)).join(', ')}`;
}
document.querySelector('#convert').addEventListener('click', convert);
convert();
</script>
</body>
</html>
Enter 0xD83D, 0xDE42 to see 🙂 and the decimal code units 55357, 56898. Try 65536 or 65,,66 to see the input message.




