fromCodePoint Rebuilds Emoji Sequences
String.fromCodePoint accepts surrogate pairs and ZWJ sequences while length counts UTF-16 units, so one emoji can span two units or five across joins.
Code
const label = String.fromCodePoint(0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467);
console.log('rendered:', label);
console.log('UTF-16 code units (str.length):', label.length);
console.log('code points ([...str]):', [...label].length);
// 单个码点超过 BMP 会占两个 UTF-16 单元
console.log('single emoji:', String.fromCodePoint(0x1f600).length, [...'\u{1f600}'].length);
// 混合 BMP 与增补平面:length 与码点数分道扬镳
const mixing = String.fromCodePoint(65, 0x1f600, 66);
console.log('mixed ABC+emoji length:', mixing.length, 'code points:', [...mixing].length);Output
Captured from a sandboxed run of this snippet in the build pipeline.
rendered: 👨👩👧
UTF-16 code units (str.length): 8
code points ([...str]): 5
single emoji: 2 1
mixed ABC+emoji length: 4 code points: 3The shape of the logic
Top-level bindings label, mixing hold the demo state — nothing sits inside a function scope.
Watch out for
- Zero packages pulled in — the runtime ships every API this code touches.
Putting it to use
The file’s steps show up in the output as rendered:, UTF-16 code units (str.length):, code points ([...str]):.
Core lines
const label = String.fromCodePoint(0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467);
const mixing = String.fromCodePoint(65, 0x1f600, 66);
Interactive Tools for This Topic
ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Text ReverserReverse text by characters, words, or lines, counting Unicode code points so emoji stay intact.
FAQ
- Which packages does this snippet need?
- None. Every symbol the code uses resolves inside the standard library. Its state lives in module-level bindings: `label`, `mixing`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.