Re:HTML comments work in JavaScript too
by eyelidlessness on Tuesday March 08, 2022 @12:21PM
(#30602587)
In useful code sure. But `<!-f` with its dynamic casting implications may be useful in code golf. I haven’t golfed in years so I’m not sure, but I can imagine it having some benefit I haven’t though of.
[ Parent
| on HN ]
You may be right. Unless it's true that `<!-f` is always equivalent to `<!f`.
Then again, I'd be even more horrified if the above statement is not true anyways.
Re:HTML comments work in JavaScript too
by eyelidlessness on Tuesday March 08, 2022 @12:54PM
(#30603136)
Had to try it! Here is some horror for your day: https://codepen.io/eyelidlessness/pen/abVrjQq
Re:HTML comments work in JavaScript too
by LordDragonfang on Tuesday March 08, 2022 @05:49PM
(#30608374)
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)
Re:HTML comments work in JavaScript too
by eyelidlessness on Tuesday March 08, 2022 @07:30PM
(#30609435)
While we’re at it, here’s some more madness… this time with booleans and numbers!
This one really gets to me.
f = 0 (type: object, constructor: Number)
0<!f = false
0<!-f = true
Re:HTML comments work in JavaScript too
by eyelidlessness on Tuesday March 08, 2022 @11:50PM
(#30611060)
I’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
It'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). `!-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.
Re:HTML comments work in JavaScript too
by LordDragonfang on Wednesday March 09, 2022 @01:15PM
(#30618018)
See also:
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.
this is <-!, not <!-
It's 0<!-f in the code, just not in the html text
Re:HTML comments work in JavaScript too
by eyelidlessness on Tuesday March 08, 2022 @05:42PM
(#30608314)
Thanks both, fixed! That’s what I get for coding on my phone
The two are certainly not always equivalent (for example if f is an object). `<!-f' might be always equivalent to `<!+f', though.
<! is less than or not equal right? which is a strict subset of !=, since anything < by definition is !=
Re:HTML comments work in JavaScript too
by dragonwriter on Tuesday March 08, 2022 @05:49PM
(#30608380)
> <! is less than or not equal right?
No, it is less-than-not, not less-than-or-not-equal-to.
That is <!x is the same as < (!x); if x is true-ish, it is “< false” and if x is false-ish it is “< true”.