简体   繁体   中英

What is the meaning of “<-” in JS

in the following code snippet I'd expect an error, but false is returned. Why?

foo = {}
foo <- "lel"

returns false

It certainly is not comparing which one is larger, so what is it doing?

<- doesn't mean anything as a single token. You have < (less than) followed by - (unary - = negation) applied to "lel" . Eg:

foo < -"lel"

It's false because -"lel" is NaN *, and all comparisons with NaN are false (even equality, eg NaN === NaN is false).

(It happens that foo gets coerced to number by the < as well [that's what < and > do when one of the operands is of type number and the other isn't]. And that coercing {} to number also yields NaN . So the final step is NaN < NaN which is false because, again, all comparisons with NaN are false.)


* ...because applying a unary - or + to a string coerces that string to number; "lel" coerces to NaN , and then negating that gives you NaN because like comparisons, all math ops on NaN result in NaN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM