What is String.fromCodePoint()?
String.fromCodePoint() is a powerful static method in JavaScript that creates a string from a sequence of Unicode code points. Unlike its predecessor String.fromCharCode(), this method can handle the full Unicode range, including supplementary characters like emojis, mathematical symbols, and characters from non-Latin scripts. Understanding how to use fromCodePoint() is essential for working with internationalization, emoji handling, and modern web applications that support diverse languages and symbols.
This guide covers everything you need to know about fromCodePoint(), from basic syntax to advanced practical applications. For teams building multilingual web applications, proper Unicode handling is a critical skill that separates amateur code from professional-grade software.
What makes fromCodePoint() essential for modern JavaScript
Full Unicode Support
Handles all 1,114,112 Unicode code points (0 to 0x10FFFF), including supplementary characters
Emoji Ready
Directly create emoji and special symbols without manual surrogate pair calculation
International Characters
Support for Chinese, Japanese, Arabic, Cyrillic, and all world writing systems
Error Handling
Throws RangeError for invalid inputs, preventing silent bugs in your code
Understanding Unicode and Code Points
Before diving into fromCodePoint(), it's crucial to understand the underlying Unicode standard that JavaScript uses for text encoding.
What Are Unicode Code Points?
Unicode code points are numerical values assigned to characters in the Unicode standard. Think of them as unique identifiers for every character, letter, emoji, or symbol used in written language.
- Each character has a unique code point, typically shown as U+ followed by a hexadecimal value
- Example: U+0041 represents the letter 'A', U+1F600 represents π (grinning face)
- The Unicode standard covers 150+ writing systems with over 140,000 characters
- Code points range from 0 to 1,114,111 (0x10FFFF)
UTF-16 Encoding in JavaScript
JavaScript uses UTF-16 encoding to represent strings internally:
- Basic Multilingual Plane (BMP): Code points 0-65,535 (U+0000 to U+FFFF) fit in a single 16-bit unit
- Supplementary Planes: Code points 65,536-1,114,111 require two 16-bit units called surrogate pairs
- Surrogate pairs consist of a high surrogate (0xD800-0xDBFF) and a low surrogate (0xDC00-0xDFFF)
This is where fromCodePoint() shines--it automatically handles the conversion from full code points to the appropriate UTF-16 representation, including surrogate pairs when needed. For more on character encoding fundamentals, see our guide to character encoding in web development.
Syntax and Parameters
Method Signature
String.fromCodePoint()
String.fromCodePoint(num1)
String.fromCodePoint(num1, num2)
String.fromCodePoint(num1, num2, /* ..., */ numN)
Parameters
num1, ..., numN Required. One or more integers representing Unicode code points.
- Valid range: 0 to 1,114,111 (0x10FFFF)
- Can be specified in decimal or hexadecimal notation
- Each value must be a valid integer within the specified range
Return Value
Returns a new string created from the specified sequence of code points. The string's length (in UTF-16 code units) may exceed the number of arguments because supplementary characters require two code units.
Exceptions
RangeError is thrown when any provided number is:
- Not an integer
- Less than 0
- Greater than 1,114,111 (0x10FFFF) after conversion to a number
1// Basic usage2String.fromCodePoint() // ""3String.fromCodePoint(65) // "A"4String.fromCodePoint(65, 66, 67) // "ABC"5 6// Hexadecimal notation7String.fromCodePoint(0x0041) // "A"8String.fromCodePoint(0x1F600) // "π"9 10// Multiple code points11String.fromCodePoint(0x0041, 0x0042, 0x0043) // "ABC"12 13// Invalid inputs throw RangeError14String.fromCodePoint(-1) // RangeError15String.fromCodePoint(0x110000) // RangeError16String.fromCodePoint(3.14) // RangeError17String.fromCodePoint(NaN) // RangeErrorfromCodePoint() vs fromCharCode()
Understanding the differences between these two methods is crucial for writing correct JavaScript code that handles Unicode properly.
Key Differences
| Aspect | fromCodePoint() | fromCharCode() |
|---|---|---|
| Code Point Range | 0 to 0x10FFFF | 0 to 0xFFFF |
| Supplementary Characters | Supported directly | Requires surrogate pairs |
| Error Handling | RangeError thrown | Silent truncation |
| Introduced | ES2015 (ES6) | ES1 (original) |
BMP Characters (Basic Multilingual Plane)
Both methods work identically for characters in the BMP range:
String.fromCodePoint(65, 66, 67) // "ABC"
String.fromCharCode(65, 66, 67) // "ABC"
String.fromCodePoint(0x0404) // "Π" (Cyrillic)
String.fromCharCode(0x0404) // "Π"
Supplementary Characters (Emojis, Symbols)
This is where the methods diverge significantly:
fromCodePoint() handles supplementary characters directly:
String.fromCodePoint(0x1F303) // "π" (Night with Stars)
String.fromCodePoint(0x1F600) // "π" (Grinning Face)
String.fromCodePoint(0x1D306) // "π" (Musical Symbol)
fromCharCode() requires manual surrogate pair calculation:
String.fromCharCode(0xD83C, 0xDF03) // "π" (Night with Stars)
String.fromCharCode(55356, 57091) // "π" (decimal surrogates)
String.fromCharCode(0xD834, 0xDF06) // "π" (Musical Symbol)
Error Handling Comparison
// fromCodePoint() throws RangeError for invalid inputs
String.fromCodePoint("_") // RangeError: Invalid code point
String.fromCodePoint(Infinity) // RangeError: Invalid code point
String.fromCodePoint(-1) // RangeError: Invalid code point
String.fromCodePoint(3.14) // RangeError: Invalid code point
// fromCharCode() silently truncates (dangerous!)
String.fromCharCode("_") // "?" (silently fails)
String.fromCharCode(Infinity) // "\u0001" (truncated)
String.fromCharCode(-1) // "\uFFFF" (truncated)
String.fromCharCode(3.14) // "\u0003" (truncated)
Practical Use Cases
1. Emoji Handling
Modern applications frequently work with emoji, especially in social features and messaging. When building AI-powered chatbots that interact with users, proper emoji handling enhances the user experience:
// Common emojis
String.fromCodePoint(0x1F600) // "π" (grinning face)
String.fromCodePoint(0x1F3FB) // "π»" (light skin tone)
String.fromCodePoint(0x1F926, 0x1F3FC) // "π€¦π»" (face palm with skin tone)
// Family emojis with skin tones
String.fromCodePoint(0x1F469, 0x1F3FF, 0x200D, 0x1F692) // "π©πΏβπ"
// Multiple emoji in sequence
String.fromCodePoint(0x1F44D, 0x1F44D, 0x1F44D) // "πππ"
2. Internationalization (i18n)
Supporting languages beyond the Latin alphabet is essential for global web applications:
// Chinese characters
String.fromCodePoint(0x4E2D, 0x6587) // "δΈζ" (Chinese)
// Japanese characters
String.fromCodePoint(0x65E5, 0x672C, 0x8A9E) // "ζ₯ζ¬θͺ" (Japanese)
// Arabic text (right-to-left)
String.fromCodePoint(0x0627, 0x0644, 0x0639, 0x0631, 0x0628, 0x064A, 0x0629)
// "Ψ§ΩΨΉΨ±Ψ¨ΩΨ©" (Arabic)
// Hindi/Devanagari
String.fromCodePoint(0x0939, 0x093F, 0x0928, 0x094D, 0x0926) // "ΰ€Ήΰ€Ώΰ€¨ΰ₯ΰ€¦" (Hindi)
// Thai
String.fromCodePoint(0x0E2A, 0x0E2D, 0x0E25, 0x0E22, 0x0E32, 0x0E41, 0x0E2A, 0x0E21)
// "ΰΈͺΰΈ§ΰΈ±ΰΈͺΰΈΰΈ΅" (Thai - Hello)
3. Mathematical and Technical Symbols
Accessing specialized symbols for technical documentation:
// Mathematical operators
String.fromCodePoint(0x2200) // "β" (for all)
String.fromCodePoint(0x2203) // "β" (there exists)
String.fromCodePoint(0x2211) // "β" (summation)
String.fromCodePoint(0x221A) // "β" (square root)
String.fromCodePoint(0x221E) // "β" (infinity)
// Greek letters (used in mathematics)
String.fromCodePoint(0x03B1) // "Ξ±" (alpha)
String.fromCodePoint(0x03B2) // "Ξ²" (beta)
String.fromCodePoint(0x03C0) // "Ο" (pi)
String.fromCodePoint(0x03B8) // "ΞΈ" (theta)
String.fromCodePoint(0x03BB) // "Ξ»" (lambda)
String.fromCodePoint(0x03C3) // "Ο" (sigma)
// Arrows and geometric shapes
String.fromCodePoint(0x2192) // "β" (right arrow)
String.fromCodePoint(0x2194) // "β" (left-right arrow)
String.fromCodePoint(0x25A0) // "β " (black square)
String.fromCodePoint(0x25B2) // "β²" (black up-pointing triangle)
4. Dynamic String Generation
Creating strings programmatically from character codes:
// Generate alphabet
const uppercaseLetters = [];
for (let i = 65; i <= 90; i++) {
uppercaseLetters.push(String.fromCodePoint(i));
}
uppercaseLetters.join('') // "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Generate circled numbers
const circledNumbers = Array.from({length: 20}, (_, i) =>
String.fromCodePoint(0x2460 + i)
);
// ["β ", "β‘", "β’", ..., "β³"]
// Generate superscript numbers
const superscripts = [0x2070, 0x00B9, 0x00B2, 0x00B3, 0x2074];
superscripts.map(cp => String.fromCodePoint(cp)).join('')
// "β°ΒΉΒ²Β³β΄"
// Generate subscript numbers
const subscripts = [0x2080, 0x2081, 0x2082, 0x2083, 0x2084];
subscripts.map(cp => String.fromCodePoint(cp)).join('')
// "βββββ"
5. Converting from External Data
Working with Unicode data from APIs or databases:
// Parse Unicode escape sequences
function parseUnicodeEscapes(str) {
const matches = str.match(/\\u([0-9A-Fa-f]{4,5})/g) || [];
return matches.map(m => String.fromCodePoint(parseInt(m.slice(2), 16)));
}
// Convert array of code points to string
function codePointsToString(codePoints) {
return String.fromCodePoint(...codePoints);
}
// Example
codePointsToString([0x0041, 0x0042, 0x0043]) // "ABC"
// Validate and convert user input
function safeCodePointInput(input) {
const codePoint = parseInt(input, 10);
if (Number.isInteger(codePoint) && codePoint >= 0 && codePoint <= 0x10FFFF) {
return String.fromCodePoint(codePoint);
}
return null;
}
Best Practices and Common Patterns
Input Validation
Always validate input before using fromCodePoint() to ensure robust code:
function safeFromCodePoint(...args) {
// Filter to valid integers within range
const validCodePoints = args.filter(n =>
Number.isInteger(n) && n >= 0 && n <= 0x10FFFF
);
if (validCodePoints.length !== args.length) {
console.warn('Some values were filtered out as invalid code points');
}
return String.fromCodePoint(...validCodePoints);
}
Error Handling Pattern
Implement robust error handling for user input:
function createStringFromInput(input) {
try {
const codePoint = parseInt(input, 10);
return String.fromCodePoint(codePoint);
} catch (error) {
if (error instanceof RangeError) {
return 'Invalid code point';
}
throw error; // Re-throw unexpected errors
}
}
Performance Optimization
When processing multiple code points, batch them into a single call:
// β
Efficient: single call with spread
const codePoints = [9731, 9733, 9842, 0x2F804];
const result = String.fromCodePoint(...codePoints); // "β
β²δ½ "
// β Less efficient: multiple calls create intermediate strings
let result2 = '';
for (const cp of codePoints) {
result2 += String.fromCodePoint(cp); // Creates new string each iteration
}
Common Mistakes to Avoid
- Confusing decimal and hexadecimal: Remember 0x1F600 = 128512 (decimal)
- Assuming fromCharCode() handles emojis: It requires manual surrogate pair calculation
- Not validating user input: Invalid values silently fail with fromCharCode()
- Forgetting about surrogate pairs: fromCodePoint() handles them automatically
- Ignoring RangeError: Catch this exception to debug invalid code points
Integration with Modern JavaScript
For projects using TypeScript, add type safety to your utility functions:
function safeCodePointsToString(codePoints: number[]): string {
const validPoints = codePoints.filter(
(n): n is number => Number.isInteger(n) && n >= 0 && n <= 0x10FFFF
);
return String.fromCodePoint(...validPoints);
}
To learn more about building robust TypeScript applications with proper character handling, explore our web development services.
Browser Compatibility
Baseline Status
String.fromCodePoint() is classified as Baseline Widely Available, meaning it works across all modern browsers and has been available since September 2015.
Browser Support
| Browser | Version | Release Date |
|---|---|---|
| Chrome | 41+ | April 2015 |
| Firefox | 29+ | March 2014 |
| Safari | 9+ | September 2015 |
| Edge | 12+ | July 2015 |
| Opera | 28+ | August 2015 |
Legacy Browser Support
For projects requiring Internet Explorer support:
- Not supported in IE (all versions)
- Use Babel with @babel/plugin-transform-es2015-template-literals
- Consider a polyfill for fromCodePoint()
- Alternative: Use fromCharCode() with manual surrogate pair handling as fallback
Node.js Support
Full support in Node.js 4.0.0 and later versions. For enterprise applications, ensure your JavaScript development environment handles legacy browser requirements appropriately.
Frequently Asked Questions
Summary
String.fromCodePoint() is an essential method for modern JavaScript development, particularly when working with:
- Internationalized text and multilingual applications
- Emoji and special symbols in user interfaces
- Unicode characters from any writing system worldwide
- Data processing that may include supplementary characters
Key Takeaways
- Use fromCodePoint() for new code -- it handles the full Unicode range correctly
- It throws RangeError for invalid inputs -- helpful for debugging
- Automatic surrogate pair handling -- no manual calculation needed
- Works in all modern browsers since 2015 -- baseline compatible
- Essential for emoji and international character support -- required for modern web apps
When working with Unicode in JavaScript, always prefer String.fromCodePoint() for its correctness, clarity, and robust error handling. For teams building global applications, mastering this method is a fundamental step toward professional-grade internationalization support. Contact our web development team to learn how we can help you implement proper Unicode handling in your projects.
Sources
- MDN Web Docs: String.fromCodePoint() - Official JavaScript documentation
- MDN Web Docs: String.fromCharCode() - Related method documentation
- ECMAScript 2026 Language Specification - Official language specification