0

In javascript, especially in JSON, we can represent unicode characters with escape sequences or direct unicode string.

How does the two differ? Is there any practical implications or pitfalls in using one over another?

1 Answers1

1

Escape sequences use ASCII characters so can be represented without having to worry about the encoding that the JS or JSON is transmitted / saved using. They require more bytes per encoded character. They aren't easy for a human to read by looking at source code.

None of the above are true when using a Unicode encoding (such as UTF-8).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    So, if we use a non-UTF-8 encoding with escape, will javacript be able to correctly display the correct unicode characters? Also are you implying that with UTF-8 , using one over another has no practical benefits or pitfalls? – dreamingblackcat Jul 26 '15 at 15:59
  • As I said, the characters used to describe the escape sequence are just ASCII. The encoding you used to represent the `\` and the characters that immediately follow it just has to be ASCII or a superset of ASCII (which is, AFAIK, all encodings). – Quentin Jul 26 '15 at 16:02
  • "Also are you implying that with UTF-8 , using one over another has no practical benefits or pitfalls? " — I gave two drawbacks of using escape sequences in the first paragraph of the answer, so no. – Quentin Jul 26 '15 at 16:02
  • 1
    Using UTF-8 has the possible pitfall of the client not accepting that encoding (not likely nowadays). And when you unescape the ASCII-encoded string you get the UTF-8 version. – Jan Jul 26 '15 at 16:02
  • Clients that don't support UTF-8 are rarer than hen's teeth. You don't necessarily get the UTF-8 version when you parse the JSON, you get whatever the program you are using is working with internally. It will be the same *characters* though, just represented in different ways in memory. – Quentin Jul 26 '15 at 16:04