Coder Perfect

In JavaScript, add a Unicode character.

Problem

On my html page, I need to add an Omega (). I’m doing it with HTML escaped code, so I can type Ω and get. That’s all fine and well when I put it into a HTML element; however, when I try to put it into my JS, e.g. var Omega = Ω, it parses that code as JS and the whole thing doesn’t work. Is there anyone who knows how to go about doing this?

Asked by Bluefire

Solution #1

I’m assuming you want Omega to be a string that has an uppercase omega? If that’s the case, you could write:

var Omega = '\u03A9';

(Because is the Unicode letter U+03A9; that is, 03A9 is 937, but expressed as four hexadecimal digits.)

Answered by ruakh

Solution #2

Despite @ruakh’s excellent response, I’ll add additional options for completeness:

Even var Omega = ‘Ω’ could be used in JavaScript, but only if your JavaScript code is:

In certain circumstances, the code will be analyzed by an HTML parser first (before being given to the JavaScript interpreter) to ensure that character references such as Ω are recognized. In most circumstances, the constraints make this an unfeasible method.

You can alternatively type the character directly, as in var Omega = “, but the character encoding must support it, the encoding must be properly specified, and you’ll need software that supports it. This is a simple method that is doable if you use UTF-8 encoding for everything and are willing to cope with the problems it causes. The source code will be readable, and instead of code notations, you will see the character itself. On the other side, if other people start working with your code, you can get some unexpected results.

The u notation, as in var Omega = ‘u03A9,’ works regardless of character encoding and is almost universal in practice. However, it can only be used up to U+FFFF, i.e. uffff, but most characters that most people have ever heard of fall into that range. (You can use surrogate pairs or one of the two ways above if you need “higher” characters.)

You may also create a character with the String.fromCharCode() function, which accepts a Unicode number as an input, either in decimal (var Omega = String.fromCharCode(937)) or in hexadecimal (var Omega = String.fromCharCode(937)) (0x3A9). Up to U+FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF This method can be used even if the Unicode number is stored in a variable.

Answered by Jukka K. Korpela

Solution #3

One way is to write the character’s name in the screenplay, for example:

const omega = 'Ω';

This necessitates informing the browser of the right source encoding; see Unicode in JavaScript for further information.

If you can’t or won’t do this (for example, because the character is too exotic to be found in the code editor font), the safest approach might be to use new-style string escape or String.fromCodePoint:

const omega = '\u{3a9}';

// or:

const omega = String.fromCodePoint(0x3a9);

This is not limited to UTF-16 and can be used with any unicode code point. The disadvantages of the alternative options discussed here are as follows:

Answered by coldfix

Solution #4

The answer is correct, but you don’t need to declare a variable. A string can contain your character:

"This string contains omega, that looks like this: \u03A9"

Unfortunately, those ASCII values are still required for displaying UTF-8, but I am still waiting (for far too long…) for the day when UTF-8 will be the same as ASCII, and ASCII will be a thing of the past.

Answered by fresko

Post is based on https://stackoverflow.com/questions/13093126/insert-unicode-character-into-javascript