ChatGPT解决这个技术问题 Extra ChatGPT

How to toggle a boolean?

Is there a really easy way to toggle a boolean value in javascript?

So far, the best I've got outside of writing a custom function is the ternary:

bool = bool ? false : true;

J
Jordan
bool = !bool;

This holds true in most languages.


really nice solution, i was using: test = (test == true)? false : true;
This need initialisation, so is there a way without initialisation. Though this would work on object attributes.
@user2846569, if you're toggling the boolean, that means that by nature the variable has already been initialized. You could expand it to bool = !bool || true; to have a default, I suppose.
@Jordan That causes ReferenceError
J.B wants a unary statement rather than binary statement. Perhaps it could be bool!! or !!bool. But so far i've never seen such syntax. I agree with Jordan. It is so short already.
N
NearHuscarl

If you don't mind the boolean being converted to a number (that is either 0 or 1), you can use the Bitwise XOR Assignment Operator. Like so:

bool ^= true;   //- toggle value.

let inDynamicEditMode   = true;     // Value is: true (boolean)
inDynamicEditMode      ^= true;     // Value is: 0 (number)
inDynamicEditMode      ^= true;     // Value is: 1 (number)
inDynamicEditMode      ^= true;     // Value is: 0 (number)

This is easier for me to scan than repeating the variable in each line.

This method works in all (major) browsers (and most programming languages).


Since this recasts to an integer anyway, it can be simplified as: bool ^= 1
As the value is casted to a number, you will be not able to do bool === false or bool === true anymore. It might break existing code, so be careful.
F
Feeco
bool = bool != true;

One of the cases.


x
xameeramir

Let's see this in action:

var b = true; console.log(b); // true b = !b; console.log(b); // false b = !b; console.log(b); // true

Anyways, there is no shorter way than what you currently have.


O
OfirD

I was searching after a toggling method that does the same, but which "toggles" an initial value of null or undefined to false.

Here it is:

booly = !(booly != false)

A
Ardhi
bool === tool ? bool : tool

if you want the value to hold true if tool (another boolean) has the same value


G
Grant

In a case where you may be storing true / false as strings, such as in localStorage where the protocol flipped to multi object storage in 2009 & then flipped back to string only in 2011 - you can use JSON.parse to interpret to boolean on the fly:

this.sidebar = !JSON.parse(this.sidebar);

In case the variable has a different values for true/false like "Yes"/"No", I use sidebar = (sidebar == trueValue) falseValue : trueValue;
R
Rafael Lucas

I always liked Boolean value, but nowadays I'm using binary for both convenience and debugging. You can use de consept !key : ++key: --key to toggle, and if you are in any asynchronous function or anytime an error or false true occurs, the value will leak out of 0(zero)/ 1(one) and you can trigger an alert to debug later.