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.

JavascriptData Integrity

Code

javascriptsnippet
11 lines
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: 3

The 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

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`.