jsguides

String.fromCodePoint()

String.fromCodePoint(...codePoints)

String.fromCodePoint() is a static method that returns a string built from one or more Unicode code points. Unlike the older String.fromCharCode(), it correctly handles code points above U+FFFF (supplementary characters such as emoji and rare scripts).

Syntax

String.fromCodePoint(num1)
String.fromCodePoint(num1, num2)
String.fromCodePoint(num1, num2, /* …, */ numN)

Parameters

ParameterTypeDefaultDescription
…codePointsNumberrequiredOne or more Unicode code points (integers between 0 and 0x10FFFF).

Return Value

A string formed by the characters corresponding to the specified code points. Throws a RangeError if any code point is not a valid Unicode value (e.g. negative or > 0x10FFFF).

Examples

Basic ASCII characters

The examples above demonstrate the basic usage with ASCII code points. Passing decimal values for common characters like ‘A’ (65) or symbols like ’☃’ (9731) produces the expected single-character strings. Each call can accept multiple code points, building the result from left to right:

String.fromCodePoint(65, 66, 67);
// 'ABC'

String.fromCodePoint(9731, 9733, 9842);
// '☃★♲'

Emoji (above U+FFFF)

What sets fromCodePoint() apart is its ability to handle code points above U+FFFF — the range where emoji and supplementary scripts live. Pass a value like 0x1F600 (😀) and the method produces the correct two-code-unit surrogate pair automatically. You can use either hex or decimal notation:

String.fromCodePoint(0x1F600);
// '😀'

String.fromCodePoint(0x1F4A9);
// '💩'

String.fromCodePoint(128512);
// '😀'  (128512 === 0x1F600)

Multiple code points

A single call to fromCodePoint() can produce an entire string when you pass code points for each character. This is cleaner than calling the method once per character and concatenating the results. The arguments are processed in order, so 72, 101, 108, 108, 111 builds “Hello” character by character:

String.fromCodePoint(72, 101, 108, 108, 111);
// 'Hello'

Invalid code point throws RangeError

Not every integer is a valid code point. The method enforces the Unicode range (0 to 0x10FFFF) and throws a RangeError when a value falls outside it. Negative numbers and values above 1,114,111 both trigger the error. This guard catches mistakes early rather than producing garbage output:

String.fromCodePoint(-1);
// RangeError: Invalid code point -1

String.fromCodePoint(0x110000);
// RangeError: Invalid code point 1114112

Common Patterns

Encoding emoji by name

The raw code point values work, but readable code benefits from named constants. Storing code points in an object lets you reference emoji by a descriptive key, turning numeric literals into self-documenting lookups. This pattern is common in UI libraries and command-line tools that need to map user-facing names to symbols:

const EMOJI = {
  smile:  0x1F600,
  heart:  0x2764,
  thumbUp: 0x1F44D,
};

function emoji(name) {
  return String.fromCodePoint(EMOJI[name]);
}

console.log(emoji('smile'));    // 😀
console.log(emoji('heart'));    // ❤
console.log(emoji('thumbUp')); // 👍

Converting code point array to string

When you have a list of code points stored in an array, the spread operator feeds them directly into fromCodePoint(). This avoids a manual loop and keeps the conversion in a single expression. The approach works for ASCII, symbols, and supplementary characters alike:

const codePoints = [72, 101, 108, 108, 111, 32, 127758];

const result = String.fromCodePoint(...codePoints);
console.log(result);
// 'Hello 🌎'

Generating a Unicode range

Generating sequential ranges is useful for building lookup tables or test fixtures. But the most important distinction between fromCodePoint() and the older String.fromCharCode() is how they handle code points above the Basic Multilingual Plane. The following comparison makes the difference clear:

// Generate all uppercase Latin letters
const letters = Array.from({ length: 26 }, (_, i) =>
  String.fromCodePoint(65 + i)
);

console.log(letters.join(',')); // A,B,C,...,Z

fromCodePoint() vs fromCharCode()

String.fromCharCode() predates Unicode supplementary planes and only accepts 16-bit values (0–65535). For characters with code points above U+FFFF — emoji, mathematical alphanumeric symbols, and many historical scripts — you would have to pass the two surrogate code units manually, which is error-prone. String.fromCodePoint() accepts the full Unicode range and converts supplementary code points to their correct two-code-unit surrogate pair automatically.

// Emoji code point 0x1F600 (😀) is above 0xFFFF
String.fromCharCode(0x1F600);           // '? ' — wrong (only low 16 bits)
String.fromCodePoint(0x1F600);          // '😀' — correct

Large arrays and the spread limit

When converting a very large array of code points with String.fromCodePoint(...array), the spread operator passes all elements as individual function arguments. JavaScript engines have a maximum argument count limit (typically around 65,536). For arrays larger than this, use Array.prototype.reduce() or process in chunks.

What a code point is

A Unicode code point is an integer in the range 0 to 1,114,111 (0x10FFFF) that identifies a character in the Unicode standard. Code points above U+FFFF — the Basic Multilingual Plane boundary — require two UTF-16 code units (a surrogate pair) to represent in JavaScript strings. String.fromCodePoint() handles this automatically: pass a code point above 0xFFFF and it produces the correct two-character string, whereas String.fromCharCode() would require you to manually compute and pass the surrogate pair values.

Why Use fromCodePoint()

String.fromCodePoint() is the right choice whenever your code works with modern Unicode text, emoji, or scripts outside the Basic Multilingual Plane. It removes the mental overhead of surrogate pairs and keeps the code closer to the Unicode model itself. If you are building text utilities, generating symbols, or converting numeric code point data back into visible characters, this method is usually the clearest option.

See Also