String.fromCodePoint()

Builds a string from a sequence of Unicode code points, including astral characters like emoji.

Signature
String.fromCodePoint()
String.fromCodePoint(num1)
String.fromCodePoint(num1, num2)
String.fromCodePoint(num1, num2, /* …, */ numN)
  • Added in JavaScript ES2015

Parameters

ParameterTypeDescription
$num1, …, numNoptionalnumberIntegers between 0 and 0x10FFFF (1114111) inclusive, each representing a Unicode code point.

Return value

stringA string created from the specified sequence of code points. Its length may exceed the number of arguments, because code points above 0xFFFF occupy two UTF-16 code units.

Errors

Throws a RangeError if an argument is not an integer, is less than 0, or is greater than 0x10FFFF after numeric conversion.

String.fromCodePoint() is the Unicode-correct counterpart to String.fromCharCode(). It accepts full code points across the whole range up to U+10FFFF and, for anything above U+FFFF, emits the surrogate pair for you. It is a static method on String.

When to reach for it

Use it whenever the numbers you hold are code points rather than code units: rendering an emoji from a database column, decoding U+XXXX notation, generating a character range that crosses the Basic Multilingual Plane boundary, or reversing codePointAt().

What trips people up

The returned length is not the argument count. String.fromCodePoint(0x1F303) produces a one-character string with length === 2. Count visible characters by iterating — [...str].length — since the string iterator walks code points, not code units.

Unlike fromCharCode(), this method validates. A fractional value, a negative, or anything above 0x10FFFF throws a RangeError instead of quietly producing the wrong glyph. That is a feature: it turns a class of silent data corruption into a loud failure. Guard values you derive by arithmetic, since 0x1F600 + 0.5 throws.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Render an emoji from a stored numeric code point
  • Reverse codePointAt to rebuild text without breaking surrogate pairs
  • Generate a contiguous range of Unicode characters for a picker UI
  • Decode U+XXXX escape notation received as text

Frequently asked questions

How do I create an emoji from its Unicode code point in JavaScript?

Call String.fromCodePoint with the hexadecimal code point, for example String.fromCodePoint(0x1F600) for a grinning face. You can also write it inline as the escape "\u{1F600}" inside a string literal.

Why is my emoji's length 2 instead of 1?

Code points above U+FFFF are stored as a surrogate pair of two UTF-16 code units, and length counts code units. Spread the string with [...str] or use Intl.Segmenter to count what a reader sees as one character.

When should I use fromCodePoint instead of fromCharCode?

Use fromCodePoint whenever your numbers are Unicode code points, which is nearly always. fromCharCode operates on code units and silently truncates anything above 0xFFFF, whereas fromCodePoint handles the full range and throws on invalid input.

More examples

It validates instead of truncating

Every invalid argument raises a RangeError rather than producing a silently wrong character, which is the key behavioural difference from fromCharCode. The final line shows the top of the valid range still returns a string, occupying two code units.

Example · javascript
const bad = [1.5, -1, 0x110000, NaN];

for (const value of bad) {
  try {
    String.fromCodePoint(value);
  } catch (err) {
    console.log(value, "->", err.name);
  }
}

console.log("max valid:", String.fromCodePoint(0x10ffff).length);

Generating a character range

The same helper works on both sides of the 0xFFFF boundary because fromCodePoint handles surrogate encoding internally. The final length is twice the number of emoji, a reminder that each astral character costs two code units.

Example · javascript
const range = (from, to) =>
  Array.from({ length: to - from + 1 }, (_, i) => String.fromCodePoint(from + i));

console.log(range(0x2460, 0x2468).join(" "));  // circled digits
console.log(range(0x1f34e, 0x1f353).join(" ")); // fruit emoji
console.log(range(0x1f34e, 0x1f353).join("").length);

Discussion

  • Be the first to comment on this lesson.