Data & APIs

JavaScript Unicode Escape Sequences: UTF-16 and Emoji

Use Unicode escapes correctly, understand surrogate pairs, and avoid executing escaped input as JavaScript.

Photorealistic editorial photo for Unicode Escape Sequences
Data & APIs · Practical ideas for your next build

A Unicode escape writes a character using hexadecimal notation inside source text. In JavaScript, the traditional four-digit form represents one UTF-16 code unit. A brace-style escape can represent an entire Unicode code point. These distinctions become visible as soon as the input includes emoji or less common writing systems.

Distinguish source notation from the resulting string

A source literal containing a Unicode escape is interpreted by the JavaScript parser. Text loaded from a file or pasted into a textarea is not automatically re-parsed as a string literal. Our decoder explicitly recognizes Unicode escapes; it does not evaluate JavaScript. It leaves unrelated escape conventions unchanged so it cannot be mistaken for a general-purpose JSON parser.

Represent characters outside the basic plane

Characters above U+FFFF use two UTF-16 code units in JavaScript. The waving-hand emoji can therefore be written as a pair of four-digit escapes or one brace-style escape. JSON accepts the four-digit form but does not accept the JavaScript brace syntax. If the destination is JSON, validate the complete JSON document instead of assuming every JavaScript string notation is portable.

Avoid double interpretation

An escaped value can be escaped again when embedded in another document. Decode only the layer your workflow expects. Repeatedly decoding until a string looks readable can change intended literal text. Never use eval or Function to decode pasted strings. For structured JSON input, JSON.parse is the correct parser; for a focused Unicode conversion, use an explicit decoder that reports malformed sequences and code points outside the Unicode range.

Worked example

EXAMPLE
\u0041 → A
\ud83d\udc4b → 👋
\u{1F44B} → 👋 (JavaScript syntax)

Can I use brace-style Unicode escapes in JSON?

No. JSON uses four hexadecimal digits per Unicode escape; supplementary characters can use a surrogate pair.

Reference and further reading

Read the official documentation ↗