JS String Methods Reference

String.fromCodePoint(...codes)

String.fromCodePoint is the inverse of codePointAt: it converts numeric Unicode code points into a string. It accepts any number of arguments and encodes each into UTF-16 automatically.

Basic usage

String.fromCodePoint(97, 98, 99);      // "abc"
String.fromCodePoint(128512);          // "😀" (one argument, one code point)
String.fromCodePoint(0x1F600);         // "😀" (hex notation)
String.fromCodePoint(55357, 56832);    // "😀" (explicit surrogate pair)

fromCharCode vs fromCodePoint

The older String.fromCharCode treats every argument as a single UTF-16 unit, so building an astral character requires passing its surrogate pair manually:

String.fromCharCode(128512);        // "\uFFFD" wrong — 128512 > 16 bits
String.fromCodePoint(128512);       // "😀" correct
String.fromCharCode(0xD83D, 0xDE00) // "😀" correct, but you had to know the pair

The UTF-16 encoding, briefly

Code points up to U+FFFF are stored as one unit. Larger values are split into a high surrogate (0xD800–0xDBFF) and a low surrogate (0xDC00–0xDFFF):

function toSurrogates(cp) {
  const h = Math.floor((cp - 0x10000) / 0x400) + 0xD800;
  const l = ((cp - 0x10000) % 0x400) + 0xDC00;
  return [h, l];
}
toSurrogates(128512); // [55357, 56832]

Range errors

Values outside the Unicode range, fractional values, or negative numbers throw a RangeError rather than producing replacement characters:

String.fromCodePoint(0x110000); // RangeError
String.fromCodePoint(97.5);     // RangeError
String.fromCodePoint(-1);       // RangeError
Validate user-supplied numbers before calling fromCodePoint; a single out-of-range value aborts the whole call.

See also