363

I was baffled when a colleague showed me this line of JavaScript alerting 42.

alert(2+ 40);

It quickly turns out that what looks like a minus sign is actually an arcane Unicode character with clearly different semantics.

This left me wondering why that character doesn't produce a syntax error when the expression is parsed. I'd also like to know if there are more characters behaving like this.

17
  • 28
    @Elyasin Did you copy/paste or retype?
    – user253751
    Jul 20, 2015 at 1:48
  • 4
    This works in Visual C# as well. When pasting the strange character into the Visual Studio IDE, or when completing the statement by typing ;, the editor tends to change the strange ` ` character into a normal space, but if you undo that "auto-correction", you have the same behavior. That character has the same semantics as a space, even if it looks like a hyphen or minus (in usual fonts). Jul 20, 2015 at 8:30
  • 4
    The opposite can happen as well. Some languages supporting unicode in identifiers accept unicode characters that look like white space (in other words, you can't see them); it may even be possible to have completely invisible identifiers.
    – gnasher729
    Jul 20, 2015 at 9:45
  • 58
    (OT) Because 42 is an answer to everything? Jul 21, 2015 at 22:12
  • 4
    @Thomas the fact that the unexpected result was caused by that Unicode character was already clear.
    – GOTO 0
    Jul 23, 2015 at 14:55

6 Answers 6

472

That character is "OGHAM SPACE MARK", which is a space character. So the code is equivalent to alert(2+ 40).

I'd also like to know if there are more characters behaving like this.

Any Unicode character in the Zs class is a white space character in JavaScript, but there don't seem to be that many.

However, JavaScript also allows Unicode characters in identifiers, which lets you use interesting variable names like ಠ_ಠ.

15
  • 3
    Box-with-a-hex-code underscore box-with-a-hex-code. Which character is it meant to be?
    – user253751
    Jul 20, 2015 at 1:48
  • 12
    @immibis The last part of this answer is an emoticon available in image form at disapprovallook.com
    – Mark S.
    Jul 20, 2015 at 2:54
  • 3
    Note that not just Zs characters are considered to be white space in JavaScript. There are more: github.com/mathiasbynens/regexpu/blob/… Jul 21, 2015 at 8:12
  • 22
    My reaction when ಠ_ಠ can be used as an identifier in JS: ಠ_ಠ Jul 22, 2015 at 14:45
  • 4
    @ChrisCirefice underscore being treated as a letter is long-standing in C-style langauges. being treated as a letter is just common sense, since it's a letter. It would be a clear bug if ಠ_ಠ couldn't be used as an identifier.
    – Jon Hanna
    Jul 24, 2015 at 8:52
82

After reading the other answers, I wrote a simple script to find all Unicode characters in the range U+0000–U+FFFF that behave like white spaces. As it seems, there are 26 or 27 of them depending on the browser, with disagreements about U+0085 and U+FFFE.

Note that most of these characters just look like a regular white space.

function isSpace(ch)
{
    try
    {
        return Function('return 2 +' + ch + ' 2')() === 4;
    }
    catch(e)
    {
        return false;
    }
}

for (var i = 0; i <= 0xffff; ++i)
{
    var ch = String.fromCharCode(i);
    if (isSpace(ch))
    {
        document.body.appendChild(document.createElement('DIV')).textContent = 'U+' + ('000' + i.toString(16).toUpperCase()).slice(-4) + '    "' + ch + '"';
    }
}
div { font-family: monospace; }

7
  • 17
    U+0085 "NEL" is defined as whitespace by Unicode but has a long history of being mishandled. U+FFFE is a noncharacter with no name and no properties besides NChar and shouldn't be be considered whitespace by anything reasonable. That said, my browser disagrees with me on both points :)
    – hobbs
    Jul 20, 2015 at 6:58
  • 4
    @hobbs U+FFFE is also a \p{Default Ignorable Code Point}, not just a \p{Noncharacter Code Pount}. U+0085 has always been a \p{Whitespace} code point. The evil one is U+180E MONGOLIAN VOWEL SEPARATOR, which “recently” lost its \p{Whitespace} property. Note that \p{Pattern Whitespace} is a much smaller set, and an immutable property. But \p{Whitespace} is not.
    – tchrist
    Jul 20, 2015 at 14:47
  • 2
    FEFF is the BOM and can be treated like a "zero width no-break space" within texts. FFFE is it's endian swapped equivalent. Perhaps that's the reason some browsers treat is as whitespace. Jul 20, 2015 at 15:57
  • ecma-international.org/ecma-262/6.0/#sec-white-space (as linked from Felix King's answer) specifically calls out U+FEFF to be considered whitespace in JS source code. U+FFFE is not listed, but that strikes me as an error of omission.
    – zwol
    Jul 21, 2015 at 21:55
  • 2
    @zwol, it's not an error of omission, because there is no character U+FFFE. Treating it as whitespace is a bug. Indeed, treating it as a valid character at all is a bug in most cases. U+0085 is not white space according to the JS spect, but that spec's requiring special-casing of U+0085 to not be a new line is bizarre and arguably a bug in the spec.
    – Jon Hanna
    Jul 24, 2015 at 8:59
58

It appears that the character that you are using is actually longer than what the actual minus sign (a hyphen) is.

 
-

The top is what you are using, the bottom is what the minus sign should be. You do seem to know that already, so now let's see why Javascript does this.

The character that you use is actually the ogham space mark which is a whitespace character, so it is basically interpreted as the same thing as a space, which means that your statement looks like alert(2+ 40) to Javascript.

There are other characters like this in Javascript. You can see a full list here on Wikipedia.


Something interesting I noticed about this character is the way that Google Chrome (and possible other browsers) interprets it in the top bar of the page.

enter image description here

It is a block with 1680 inside of it. That is actually the unicode number for the ogham space mark. It appears to be just my machine doing this, but it is a strange thing.


I decided to try this out in other languages to see what happens and these are the results that I got.


Languages it doesn't work in:

Python 2 & 3

>> 2+ 40
  File "<stdin>", line 1
    2+ 40
        ^
SyntaxError: invalid character in identifier

Ruby

>> 2+ 40
NameError: undefined local variable or method ` 40' for main:Object
    from (irb):1
    from /home/michaelpri/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'

Java (inside the main method)

>> System.out.println(2+ 40);
Main.java:3: error: illegal character: \5760
            System.out.println(2+?40);
                                 ^
Main.java:3: error: ';' expected
            System.out.println(2+?40);
                                  ^
Main.java:3: error: illegal start of expression
            System.out.println(2+?40);
                                    ^
3 errors

PHP

>> 2+ 40;
Use of undefined constant  40 - assumed ' 40' :1

C

>> 2+ 40
main.c:1:1: error: expected identifier or '(' before numeric constant
 2+ 40
 ^
main.c:1:1: error: stray '\341' in program
main.c:1:1: error: stray '\232' in program
main.c:1:1: error: stray '\200' in program

exit status 1

Go

>> 2+ 40
can't load package: package .: 
main.go:1:1: expected 'package', found 'INT' 2
main.go:1:3: illegal character U+1680

exit status 1

Perl 5

>> perl -e'2+ 40'                                                                                                                                   
Unrecognized character \xE1; marked by <-- HERE after 2+<-- HERE near column 3 at -e line 1.

Languages it does work in:

Scheme

>> (+ 2  40)
=> 42

C# (inside the Main() method)

Console.WriteLine(2+ 40);

Output: 42

Perl 6

>> ./perl6 -e'say 2+ 40' 
42
21
  • 35
    Ubuntu isn't the problem. The window title font you're using is.
    – PSkocik
    Jul 20, 2015 at 0:22
  • 2
    firefox (iceweasel) and google chrome on debian seem to display the unicode char just fine, although I have gone to lengths to ensure unicode compatibility on my system. (actually, the most useful thing I did was the simplest: sudo apt-get install unicode, although only after hours of research and failed attempts)
    – sig_seg_v
    Jul 20, 2015 at 0:23
  • @PSkocik Interesting, I have had font problems on here before, so that is probably likely
    – michaelpri
    Jul 20, 2015 at 0:24
  • 51
    @PSkocik “Ubuntu isn't the problem. The window title font you're using is.” …which is “Ubuntu”. Jul 20, 2015 at 12:47
  • 1
    @PSkocik I finally fixed it :) Just needed to change the system title bar font.
    – michaelpri
    Jul 24, 2015 at 5:57
43

I guess it has to do something with the fact that for some strange reason it classifies as whitespace:

$ unicode  
U+1680 OGHAM SPACE MARK
UTF-8: e1 9a 80  UTF-16BE: 1680  Decimal: &#5760;
  ( )
Uppercase: U+1680
Category: Zs (Separator, Space)
Bidi: WS (Whitespace)
4
  • If that is a copy and paste from your terminal, I'd like to know where you found the command unicode.
    – BenjiWiebe
    Jul 20, 2015 at 15:37
  • 16
    It's from the Ubuntu package named (wait for it...) unicode by Radovan Garabík. The corresponding repo is at github.com/garabik/unicode .
    – PSkocik
    Jul 20, 2015 at 15:44
  • OK, thanks for the github link. AFAICT, it isn't in the Fedora repos.
    – BenjiWiebe
    Jul 20, 2015 at 16:04
  • @PSkocik ' '.codePointAt(0) at console will yield 5760. now google 5760 unicode.
    – Royi Namir
    Jul 22, 2015 at 7:07
6

I'd also like to know if there are more characters behaving like this.

I seem to remember reading a piece a while back about mischievously replacing semi-colons (U+003B) in someone's code with U+037E which is the Greek question mark.

They both look the same (to the extent that I believe the Greeks themselves use U+003B) but this article stated that the other one wouldn't work.

Some more information on this from Wikipedia is here: https://en.wikipedia.org/wiki/Question_mark#Greek_question_mark

And a (closed) question on using this as prank from SO itself. Not where I originally read it AFAIR though: JavaScript Prank / Joke

1

Many languages won't compile this expression, but I was curious what Rust's compiler had to say on the topic. It is notoriously strict but will often give us knowledge and wisdom with loving kindness.

So I asked it to compile this:

fn main() {
    println!("{}", (2+ 40));
}

And the compiler replied:

error: unknown start of token: \u{1680}
  |
  |     println!("{}", (2+ 40));
  |                       ^
  |
help: Unicode character ' ' (Ogham Space mark) looks like ' ' (Space), but it is not

JavaScript, on the other hand, (tested with the latest and most commonly used browser today) seems to be pretty chill about that character and simply ignores it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.