4 ms·
For anyone confused, this is actually (semi) sane behavior by javascript standards, because it relies on `-"f"` evaluating to NaN. Of course, because javascrip
by LordDragonfang 5y ago
For anyone confused, this is actually (semi) sane behavior by javascript standards, because it relies on `-"f"` evaluating to NaN.
Of course, because javascript, there are other string values of `f` for which the two statements are equal as well. (e.g for empty string both statements are true because `-"" === -0`, and for "1" both values are false)
https://codepen.io/LordDragonfang/pen/KKyLjLg https://codepen.io/LordDragonfang/pen/KKyLjLg
- eyelidlessness 5y agoWhile we’re at it, here’s some more madness… this time with booleans and numbers! https://codepen.io/eyelidlessness/pen/yLPdLey https://codepen.io/eyelidlessness/pen/yLPdLey
- srcreigh 5y agoThis one really gets to me. f = 0 (type: object, constructor: Number) 0<!f = false 0<!-f = true
- eyelidlessness 5y agoI’ve tried three times to write why I think this is, and each time I get lost in my reasoning. Alas, I’ve been on mobile for this whole thread and I don’t have any more brain cells to try to code like this tonight lol
- Leszek 5y agoIt's because `f = new Number(0)` is a boxed number (therefore it's an object), and `!f` calls ToBoolean on this object which unconditionally returns true (https://tc39.es/ecma262/#table-toboolean-conversions https://tc39.es/ecma262/#table-toboolean-conversions). `!-f` first applies unary minus, which first performs a ToNumber on the f object (which returns unboxed 0), that's negated to -0, and then ToBoolean of -0 is false.
- LordDragonfang 5y agoSee also: https://dorey.github.io/JavaScript-Equality-Table/ https://dorey.github.io/JavaScript-Equality-Table/ I particularly like the note on the "if" tab: > Note: This row does not match up with any of the rows in the other table.