544

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;
Flip
  • 6,233
  • 7
  • 46
  • 75
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258

9 Answers9

1208
bool = !bool;

This holds true in most languages.

Jordan
  • 31,971
  • 6
  • 56
  • 67
122

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.

This is especially good if you use long, descriptive boolean names, EG:
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).

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
16

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.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
13
bool = bool != true;

One of the cases.

Boris
  • 4,944
  • 7
  • 36
  • 69
5

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)
OfirD
  • 9,442
  • 5
  • 47
  • 90
2

This is an old question but I think a ES6 update will be good.

Usually we want a toggle that can handle everything without breaking our code.

We can use an initial value for null or undefined values as false.

const boolToggler = b => !(b ?? false)

let foo
console.log('foo:', foo) // undefined

foo = boolToggler(foo)
console.log('foo:', foo) // true (assumes undefined as 'false')

foo = boolToggler(foo)
console.log('foo:', foo); // false

let fee = null
console.log('fee:', fee) // null

fee = boolToggler(fee)
console.log('fee:', fee) // true (assumes null as 'false')

let faa = true
console.log('faa:', faa) // true

faa = boolToggler(faa)
console.log('faa:', faa); // false
Teocci
  • 7,189
  • 1
  • 50
  • 48
  • Isn't `!(b ?? false)` kind of redundant since it's being fed into the `!` operator? `null` and `undefined` are falsy anyway so the same behavior can be achieved with `!b` – Slackow Aug 31 '23 at 17:35
1
bool === tool ? bool : tool

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

Ardhi
  • 2,855
  • 1
  • 22
  • 31
1

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);
Grant
  • 5,709
  • 2
  • 38
  • 50
  • In case the variable has a different values for true/false like "Yes"/"No", I use `sidebar = (sidebar == trueValue) falseValue : trueValue;` – alans Feb 08 '21 at 22:29
0

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.