Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use UTF-8 character literals in JavaScript source code?

Is it save to write JavaScript source code (to be executed in the browser) which includes UTF-8 character literals?

For example, I would like to use an ellipses literal in a string as such:

var foo = "Oops… Something went wrong";

Do "modern" browsers support this? Is there a published browser support matrix somewhere?

like image 933
bloudermilk Avatar asked Mar 17 '23 20:03

bloudermilk


1 Answers

JavaScript is by specification a Unicode language, so Unicode characters in strings should be safe. You can use hex escapes (\u8E24) as an alternative. Make sure your script files are served with proper content type headers.

Note that characters beyond one- and two-byte sequences are problematic, and that JavaScript regular expressions are terrible with characters beyond the first codepage. (Well maybe not "terrible", but primitive at best.)

You can also use Unicode letters, Unicode combining marks, and Unicode connector punctuation characters in identifiers, in case you want to impress your friends. Thus

var wavy﹏line = "wow";

is perfectly good JavaScript (but good luck with your bug report if you find a browser where it doesn't work).

Read all about it in the spec, or use it to fall asleep at night :)

like image 198
Pointy Avatar answered Apr 25 '23 02:04

Pointy