Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  148] [ 4]  / answers: 1 / hits: 34469  / 9 Years ago, sun, july 19, 2015, 12:00:00

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.


More From » unicode

 Answers
16

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 ಠ_ಠ.


[#65756] Thursday, July 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;