Fwd: Boolean equivalent to pre-increment and post-icnrement

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Boolean equivalent to pre-increment and post-icnrement

Bob Myers
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)`.

Bob

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Boolean equivalent to pre-increment and post-icnrement

Sebastian Malton
Then cannot you just write 

`if (!(bool = !bool))` 

?

Sebastian Malton

Sent: August 29, 2018 4:14 PM
Subject: Fwd: Boolean equivalent to pre-increment and post-icnrement

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)`.

Bob

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Boolean equivalent to pre-increment and post-icnrement

Steve Fink
In reply to this post by Bob Myers
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.)


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss