Fwd: Boolean equivalent to pre-increment and post-icnrement
Steve Fink
sphink at gmail.com
Thu Aug 30 16:01:32 UTC 2018
On 08/29/2018 12:13 PM, Bob Myers wrote:
> In the stupid idea of the day department, for some reason I have felt
> the urge more than once in recent months for an operator which would
> invert the value of a boolean variable while evaluating to its
> pre-inversion value. For example:
>
> ```js
> if (bool!!) console.log("used to be true");
> ```
>
> The post-inversion case is less important since I can just write `if
> (bool = !bool)`.
There's always
if (counter++ & 1) console.log("used to be true");
if (++counter & 1) console.log("is now true");
or (counter++ % 2) if you prefer. And you get a free cycle counter in
the bargain! (At the cost of flatlining to false at 2**53.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180830/dad6242a/attachment.html>
More information about the es-discuss
mailing list