String.fromCodePoint()

The Unicode-correct counterpart to fromCharCode. It accepts the full code point range, encodes surrogate pairs for you, and refuses invalid input instead of truncating it.

String static methodES2015Live demo
Common call
String.fromCodePoint(0x1f600)
Returns
a string — one character per code point
Replaces
fromCharCode, for anything beyond ASCII
Watch out
an out-of-range value is a RangeError, unlike fromCharCode
String.fromCodePoint(...codePoints...codePoints — Any number of Unicode code points from 0 to 0x10FFFF. Non-integers and out-of-range values throw RangeError. With no arguments you get an empty string.type: number · default: none)
→ string

Demo

Live evaluation
Try:
Inputs
codesnumber[]code points, comma separated
Output
String.fromCodePoint(...[72, 105])
'Hi'

The second case is the whole difference: the single code point 128512 produces a complete emoji, where fromCharCode would truncate it to its low 16 bits and give an unrelated character. The surrogate pair is constructed internally, so the resulting string has length 2 even though it is one character — the encoding detail is handled, not hidden. ASCII values behave identically to fromCharCode, which is why the two are easy to confuse until non-BMP text appears.

Parameters

NameTypeRequiredDescription
...codePointsnumberno (none)Any number of Unicode code points from 0 to 0x10FFFF. Non-integers and out-of-range values throw RangeError. With no arguments you get an empty string.

Return value

string — A string built from the given code points, with surrogate pairs constructed automatically. Throws RangeError for anything outside 0 to 0x10FFFF.

Common patterns

Build a character from a hex code point
The form you see in Unicode charts.
String.fromCodePoint(0x1f600);
Round-trip with codePointAt
The two are proper inverses.
String.fromCodePoint(s.codePointAt(0)) === [...s][0];
An escape in a literal, when it is constant
No function call needed for a known character.
const grin = '\u{1F600}';

Examples

1. An emoji
String.fromCodePoint(128512)
Returns
'\u{1F600}'
2. Its length
String.fromCodePoint(128512).length
Returns
2
3. fromCharCode fails
String.fromCharCode(128512).codePointAt(0)
Returns
62976
4. Plain ASCII
String.fromCodePoint(72, 105)
Returns
'Hi'
5. Out of range throws
String.fromCodePoint(0x110000)
Returns
RangeError: Invalid code point 1114112
6. No arguments
String.fromCodePoint()
Returns
''

Pitfalls

1. The result length is not the number of arguments
Each astral code point becomes TWO code units, so one emoji gives a string of length 2. Code that assumes one argument means one unit of length — a fixed-width buffer, an index calculation — is wrong for exactly the characters this method was added to support.
Length is 2
String.fromCodePoint(128512).length
2
Count code points
[...String.fromCodePoint(128512)].length
1
2. It throws where fromCharCode truncates
A stricter contract, and a better one — but it means code migrated from fromCharCode can start throwing on input it previously mangled silently. That is the bug surfacing, not a new one.
Throws
String.fromCodePoint(0x110000)
RangeError: Invalid code point 1114112
Validate first
if (cp >= 0 && cp <= 0x10ffff) String.fromCodePoint(cp);
guarded
3. Non-integers throw too
A code point computed by division or parsed loosely may arrive fractional, and this method rejects it rather than rounding. Round or truncate deliberately before calling.
Rejected
String.fromCodePoint(65.5)
RangeError: Invalid code point 65.5
Truncate first
String.fromCodePoint(Math.trunc(65.5))
'A'
4. It is a static, not an instance method
String.fromCodePoint(...), never s.fromCodePoint(...).
Not on instances
'a'.fromCodePoint(65)
TypeError: "a".fromCodePoint is not a function
Call on String
String.fromCodePoint(65)
'A'

When to use

Use it
  • Building characters from code points of any value
  • Anything involving emoji or non-Latin scripts
  • Round-tripping with codePointAt
  • Decoding escape sequences from data
Reach for something else
  • The character is a known constant → a \u{...} escape in a literal
  • You hold UTF-16 code units already → fromCharCode is the matching inverse
  • Decoding byte data → TextDecoder
  • Very large arrays → chunk the spread, or TextDecoder

Notes

Complexity
O(n) in the number of code points
Return
A new string
CPython impl
V8: Builtins-string-fromcodepoint
Memory
Allocates the result string
Thread-safe
Single-threaded

FAQ

When the code point is computed or comes from data. For a constant character the escape is clearer and needs no function call — the method exists for the dynamic case.

const ch = '\u{1F600}';            // constant
const ch2 = String.fromCodePoint(cp); // computed

History

ES2015
fromCodePoint added alongside codePointAt to make character construction Unicode-correct.