How to toggle a boolean?
Solution 1:
bool = !bool;
This holds true in most languages.
Solution 2:
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).
Solution 3:
bool = bool != true;
One of the cases.
Solution 4:
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.