I'm really new to Javascript and I heard about unicode characters, but I don't know how they work. I did this:
alert("U+00BF");
which is the unicode for an upside-down question mark, but for some reason it just alerts the letters "U+00BF". I've tried using unicode characters with a format more like this:
alert("/xF3");
and those worked, but I don't know what I'm doing wrong with the first one. Does anyone know?
I'm really new to Javascript and I heard about unicode characters, but I don't know how they work. I did this:
alert("U+00BF");
which is the unicode for an upside-down question mark, but for some reason it just alerts the letters "U+00BF". I've tried using unicode characters with a format more like this:
alert("/xF3");
and those worked, but I don't know what I'm doing wrong with the first one. Does anyone know?
Share Improve this question asked Jan 10, 2014 at 14:23 CPCCPC 1817 silver badges18 bronze badges 3- Can you make us a fiddle sample for the code. Also i guess its happening because you are either using utf-8 charset, or not using it altogether. Try doing it with utf-16. – Vikram Tiwari Commented Jan 10, 2014 at 14:32
-
1
How e
alert("/xF3")
worked? It displays literally the four characters /xF3. – Jukka K. Korpela Commented Jan 10, 2014 at 17:17 - What I mean by "it worked" is just that the right kind of character showed up – CPC Commented Jan 10, 2014 at 19:05
2 Answers
Reset to default 6The "U+00BF"
in alert("U+00BF");
is a string of length 6 containing the characters 'U'
, '+'
, '0'
, '0'
, 'B'
, 'F'
. Hence the string "U+00BF"
is echoed out in the alert.
Based on https://developer.mozilla/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Unicode ,
You can use the Unicode escape sequence in string literals, regular expressions, and identifiers. The escape sequence consists of six ASCII characters: \u and a four-digit hexadecimal number. For example, \u00A9 represents the copyright symbol. Every Unicode escape sequence in JavaScript is interpreted as one character.
Which means, we need to do:
alert("\u00BF");
to see the "upside-down question mark" unicode character.
The notation U+00BF is simply a conventional way of emphasing that you are mentioning, in text, a Unicode character by its code number 00BF. It is not an escape notation of any kind in JavaScript.
You can use the character as such,
alert("¿")
provided that handle the character encoding issues, as you should anyway.
If, however, you find this simple approach not applicable due to some external constraints, you can use a classic JavaScript escape notation:
alert("\xBF")
for characters in the range up to U+00FF, or the Unicode-based JavaScript escape notation
alert("\u00BF")
for characters in the range up to U+FFFF. (For characters beyond that, you need a so-called surrogate pair.)
Note that the special character used in these notations is \
U+005C REVERSE SOLIDUS, monly, and originally, called “backslash”, not /
U+002F SOLIDUS, monly called “slash”, or sometimes (for emphasis) “forward slash”.