JavaScript Reference ยท String
String.fromCodePoint()
Reference for JavaScript String.fromCodePoint(): create a string from Unicode code points, including emoji, and how it differs from String.fromCharCode().
String.fromCodePoint() builds a string from one or more Unicode code points. Unlike String.fromCharCode(), it understands the full Unicode range up to U+10FFFF, so it can produce emoji and rare characters in a single argument. Characters above U+FFFF are stored by JavaScript as a pair of UTF-16 code units, and this method creates that pair for you.
It is the inverse of codePointAt(). Where the number is out of range or not a whole number, it throws instead of silently wrapping.
String.fromCodePoint(0x1f600); // "๐"
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
String.fromCodePoint() String.fromCodePoint(num1) String.fromCodePoint(num1, num2, /* โฆ, */ numN)
| Name | Description |
|---|---|
num1, โฆ, numN |
Integer code points from 0 to 0x10FFFF (1,114,111). Numeric strings are converted. |
Return value: a string made of the given code points, in order. With no arguments it returns an empty string.
Exceptions: RangeError if an argument is not an integer, is negative, is greater than 0x10FFFF, or is NaN or infinite.
Usage notes
- Static method. Call it on
String:String.fromCodePoint(...). - Length counts code units. A code point above
0xFFFFproduces a string oflength2 (a surrogate pair). Use the spread operator orfor...ofto count or iterate by code point. - Stricter than
fromCharCode(). That method wraps modulo 65536 and truncates fractions;fromCodePoint()throws aRangeError, which exposes bugs earlier. - Surrogate code points are accepted.
String.fromCodePoint(0xd800)returns a lone surrogate without error. Such strings are not well formed; test withisWellFormed()before sending them where valid Unicode is required. - A code point is not a visible character. What users see as one character (a grapheme) can combine several code points, such as a letter plus an accent or an emoji with a skin-tone modifier. Use
Intl.Segmenterto work with graphemes. - Hexadecimal literals are convenient: write
0x1f680, orparseInt("1F680", 16)when the number comes from text. - Escapes for fixed text. In source code,
"\u{1f680}"is clearer than a call when the character is a constant. - Spread limit. Spreading a very large array into the call can hit the engine's argument limit; convert in chunks.
Browser support
String.fromCodePoint() arrived with ES2015 and works in all current browsers and Node.js. It is missing in very old browsers such as Internet Explorer.