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.

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
;
, 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).