Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.
JavaScript programs are written using the Unicode character set. Unicode is a superset of ASCII and Latin-1 and supports virtually every written language currently used on the planet.
Most developers just save their JavaScript as UTF-8 unknowingly in almost all cases, which works fine. But JavaScript has to "decode" those from UTF-8 to UTF-16 for its own internal use, especially if your scripts have upper plane Unicode characters inside it.
JS does require UTF-16, because the surrogate pairs of non-BMP characters are separable in JS strings. Any JS implementation using UTF-8 would have to convert to UTF-16 for proper answers to . length and array indexing on strings. Still doesn't mean that it has to store the strings in UTF-16.
I'm guessing that you actually want Omega
to be a string containing an uppercase omega? In that case, you can write:
var Omega = '\u03A9';
(Because Ω is the Unicode character with codepoint U+03A9; that is, 03A9
is 937
, except written as four hexadecimal digits.)
Although @ruakh gave a good answer, I will add some alternatives for completeness:
You could in fact use even var Omega = 'Ω'
in JavaScript, but only if your JavaScript code is:
onclick="var Omega = 'Ω';
alert(Omega)"
or script
element inside an XHTML (or XHTML + XML) document
served with an XML content type.In these cases, the code will be first (before getting passed to the JavaScript interpreter) be parsed by an HTML parser so that character references like Ω
are recognized. The restrictions make this an impractical approach in most cases.
You can also enter the Ω character as such, as in var Omega = 'Ω'
, but then the character encoding must allow that, the encoding must be properly declared, and you need software that let you enter such characters. This is a clean solution and quite feasible if you use UTF-8 encoding for everything and are prepared to deal with the issues created by it. Source code will be readable, and reading it, you immediately see the character itself, instead of code notations. On the other hand, it may cause surprises if other people start working with your code.
Using the \u
notation, as in var Omega = '\u03A9'
, works independently of character encoding, and it is in practice almost universal. It can however be as such used only up to U+FFFF, i.e. up to \uffff
, but most characters that most people ever heard of fall into that area. (If you need “higher” characters, you need to use either surrogate pairs or one of the two approaches above.)
You can also construct a character using the String.fromCharCode()
method, passing as a parameter the Unicode number, in decimal as in var Omega = String.fromCharCode(937)
or in hexadecimal as in var Omega = String.fromCharCode(0x3A9)
. This works up to U+FFFF. This approach can be used even when you have the Unicode number in a variable.
One option is to put the character literally in your script, e.g.:
const omega = 'Ω';
This requires that you let the browser know the correct source encoding, see Unicode in JavaScript
However, if you can't or don't want to do this (e.g. because the character is too exotic and can't be expected to be available in the code editor font), the safest option may be to use new-style string escape or String.fromCodePoint
:
const omega = '\u{3a9}';
// or:
const omega = String.fromCodePoint(0x3a9);
This is not restricted to UTF-16 but works for all unicode code points. In comparison, the other approaches mentioned here have the following downsides:
const omega = 'Ω';
): only work when rendered unescaped in an HTML elementconst omega = '\u03A9';
): restricted to UTF-16String.fromCharCode
: restricted to UTF-16The 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 still those codes in ASCII are needed for displaying UTF-8, but I am still waiting (since too many years...) the day when UTF-8 will be same as ASCII was, and ASCII will be just a remembrance of the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With