Unicode Escape

A \u escape is a backslash, the letter u, and exactly four hex digits: one UTF-16 code unit. In the Basic Multilingual Plane that unit is the code point. ASCII stays as the character itself. A point above U+FFFF, including most emoji, becomes a surrogate pair of two \u sequences. Those four digits are not UTF-8 bytes and not an HTML &#x reference. A and a are the same value, and fewer than four digits is not a complete escape.

What a backslash-u escape names in a string

A \u escape plus exactly four hexadecimal digits is the JSON, JavaScript, and Java spelling of one UTF-16 code unit. For characters in the Basic Multilingual Plane, that unit is the Unicode code point, from U+0000 through U+FFFF. ASCII characters, U+0000 through U+007F, are already safe in a source file, so this form leaves them as the characters themselves. A and a in the four digits are the same value.

A code point above U+FFFF does not fit in four hex digits. UTF-16 stores it as a surrogate pair: a high unit in D800-DBFF and a low unit in DC00-DFFF. The escape form is therefore two \u sequences in a row, not one. An emoji is the usual case. A lone surrogate is not a character. The pair has to be read together.

Those four digits are not UTF-8 bytes and not an HTML character reference. The ideograph U+4E2D is \u4e2d as a code point, and the three bytes E4 B8 AD as UTF-8. Byte dumps belong with hex encode and decode. A markup parser wants a form such as 中, which is HTML escape.

How four hex digits, surrogate pairs, and other escapes differ

Why \u takes four digits and leaves ASCII characters alone

  1. Each \u introduces one UTF-16 code unit. Four hex digits cover 0000 through FFFF, which is the whole Basic Multilingual Plane.
  2. Characters at or below U+007F are already ASCII. Writing them as \u0041 instead of A does not change the character, and the common encoder keeps the literal character.
  3. Fewer than four digits is not this escape. \u4e2 is a backslash, a u, and three digits, not the code point U+04E2. Extra digits stay as the following characters.

How a character above U+FFFF becomes two \u escapes

  1. Subtract 0x10000 from the code point. The high ten bits, plus 0xD800, are the first unit. The low ten bits, plus 0xDC00, are the second.
  2. The text then contains two escapes in order, high then low. Decoding recognizes that order and rebuilds one code point. Reversing the two units does not.
  3. A high unit without a following low unit, or a low unit alone, is an unpaired surrogate. It is not an emoji and not a letter. Well-formed text never contains one.

How this escape differs from UTF-8 hex and from HTML numeric references

  1. UTF-8 encodes the same code point as one to four bytes. Hex of those bytes is two digits per byte. \u is four digits for a code unit, not for a byte.
  2. U+4E2D is one BMP character. Its UTF-8 is E4 B8 AD. Treating E4B8 as a \u value, or 4E2D as three UTF-8 bytes, produces a different string.
  3. HTML writes a code point as 中 or 中. The x, the semicolon, and the allowed length are different from \u. A markup parser does not read \u, and a JSON parser does not read &#x.

Which other backslash letters are not a \u code unit

  1. JSON also uses \n, \t, \r, \\, and \" . Those name controls and punctuation, not a four-digit code unit. A decoder that only looks for \u leaves them unchanged.
  2. Python and several C-family languages use \U plus eight hex digits for a whole code point, including characters past U+FFFF. That is one escape, not a surrogate pair of two \u escapes.
  3. \x plus two hex digits usually names one byte, not a Unicode scalar. \u00e9 is the character é. The UTF-8 bytes of é are C3 A9, which a \x spelling would list as two bytes.

Why running the escape twice does not add another backslash

  1. After one escape, the text is ASCII: a backslash, the letter u, and four hex digits. Every one of those characters is at or below U+007F.
  2. An encoder that leaves ASCII literal therefore returns the same \u text. It does not turn the backslash into another escape.
  3. Seeing the six characters backslash, u, 4, e, 2, d in a file means the escape has not been decoded. Seeing the one character U+4E2D means it has. A second encode pass cannot tell those apart once the result is already ASCII.

Where a \u sequence is a code unit and not a byte

JSON that stays in ASCII while holding non-ASCII text

JSON strings may contain raw UTF-8 or \u escapes. Both are the same characters after a JSON parse. Escaping every non-ASCII unit keeps the file in ASCII, which some logs and older transports prefer. The escapes are still code units. A later step that treats the file as a hex dump of UTF-8 bytes will not match hex encode and decode.

An emoji stored as a surrogate pair in Java or JavaScript source

Source languages that predate easy non-BMP literals write an emoji as two \u escapes. The high unit is in D800-DBFF and the low unit is in DC00-DFFF. Dropping either half, or inserting a character between them, leaves an unpaired surrogate. The pair is one code point only when the two units stay adjacent and in order.

A code point copied from a chart into the wrong escape syntax

Charts print U+1F389 or the HTML form 🎉. The \u form of that same emoji is not \u1F389, because \u only accepts four digits. It is the surrogate pair. Pasting the chart number into \u, or into a UTF-8 hex dump, uses the right number in the wrong spelling. Markup uses the HTML numeric form from HTML escape.

Common questions

What is a Unicode \u escape?
It is a backslash, the letter u, and exactly four hexadecimal digits. Those digits are one UTF-16 code unit. For a character in the Basic Multilingual Plane, that unit is the code point, such as \u4e2d for U+4E2D. ASCII characters are usually left as themselves because they need no escape. The spelling is public. It does not hide the character.
What does \u4e2d mean?
The four digits 4e2d are the hexadecimal code point U+4E2D, the ideograph 中. Case does not matter: \u4E2D is the same unit. This is not the UTF-8 byte sequence of that character, which is E4 B8 AD. It is also not the HTML reference 中. Only a reader that understands \u turns those six characters into the one ideograph.
Why does an emoji become two \u escapes?
Most emoji are code points above U+FFFF. Four hex digits cannot hold them. UTF-16 splits the point into a high surrogate and a low surrogate, and each surrogate is its own \u plus four digits. The high unit comes first. Decode them as a pair. One of the two units alone is not the emoji.
What is the difference between \u and \U?
\u plus four hex digits is one UTF-16 code unit, used by JSON, JavaScript, and Java. \U plus eight hex digits, used by Python and some C-family languages, is one whole code point and can name a character past U+FFFF in a single escape. Writing eight digits after \u does not make it a \U escape. The extra digits are just the following text.
How is a \u escape different from a hex byte dump?
\u names a code unit. A hex dump names UTF-8 bytes, two digits each. U+00E9 is \u00e9 and the single character é. The UTF-8 encoding of é is the two bytes C3 A9. Putting C3A9 after \u asks for a different code point. Putting 00E9 into a byte decoder asks for different bytes. Bytes belong with hex encode and decode.
Does \u in JSON mean the file is not UTF-8?
No. A JSON text can be UTF-8 and still contain \u escapes inside strings. The escape is an alternative spelling of a character in the string value. After parsing, \u4e2d and the raw character are the same string. Escaping is how an ASCII-only document carries non-ASCII characters. It does not change the JSON data model.
Why does a short \u sequence stay as a backslash and digits?
The escape is only complete with four hex digits. \u4e2, \u, or \uXXXX with a non-hex character is not a code unit, so a strict reader leaves those characters in place. \n and \t are different JSON escapes and are not four-digit code units either. Completing or removing the broken \u is what makes the next character a real code point.
How is \u different from an HTML character reference?
HTML writes a code point as 中 or 中, with an ampersand and a semicolon, for a markup parser. \u4e2d has a backslash and exactly four digits, for a programming-language or JSON string. A browser will not treat \u4e2d in HTML text as the ideograph. Markup references belong with HTML escape.