String.fromCodePoint vs String.fromCharCode
You'll learn how to create strings from Unicode code points correctly using the modern fromCodePoint method instead of the older fromCharCode.
When you want to build a string from numeric Unicode values, JavaScript gives you two static methods: String.fromCharCode() and String.fromCodePoint(). They sound similar, but there's a crucial difference in how they handle characters outside the Basic Multilingual Plane (BMP).
The Core Difference
String.fromCharCode() works with 16-bit code units. This means it handles characters in the BMP just fine (code points 0 to 65535), but it can't properly represent characters beyond that range—like emoji, ancient scripts, or rare symbols. If you pass it a code point above 65535, it simply truncates the value.
String.fromCodePoint() works with full Unicode code points. It correctly handles any valid Unicode character, including those that require surrogate pairs. This is the modern, correct way to create strings from Unicode values.
Example
// BMP character (works with both)
String.fromCharCode(65); // "A" ✓
String.fromCodePoint(65); // "A" ✓
// Beyond BMP (emoji: code point 128513)
String.fromCharCode(128513); // "" (incorrect, truncated)
String.fromCodePoint(128513); // "😁" ✓
// Multiple code points
String.fromCodePoint(72, 101, 108, 108, 111); // "Hello"
The Analogy
Think of fromCharCode as an old phone that only handles basic calls, while fromCodePoint is a modern smartphone that handles everything—calls, video, emoji, and more. Always choose the tool that supports the full range.
Key Takeaway: Use String.fromCodePoint() when creating strings from Unicode values—it correctly handles all characters, including those beyond the BMP that require surrogate pairs.