It seems that the character you are using is longer than the minus sign (hyphen).
-
The top shows what you're using, the bottom shows the correct minus sign. You already know this, so let's explore why Javascript behaves this way.
The character you're using is actually the ogham space mark, a whitespace character interpreted as a space by Javascript. So your statement looks like alert(2+ 40)
to Javascript.
Javascript has other characters like this; see a full list here on Wikipedia.
I noticed an interesting behavior of this character in Google Chrome and possibly other browsers' address bars.
The bar displays a block with 1680
inside - the unicode number for the ogham space mark. It might just be my machine, but it's peculiar.
I tested this in various languages with these results:
Languages where it fails:
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 where it works:
Scheme
>> (+ 2 40)
=> 42
C# (inside the Main()
method)
Console.WriteLine(2+ 40);
Output: 42
Perl 6
>> ./perl6 -e'say 2+ 40'
42