How to toggle a boolean?

 

The logical NOT operator in Boolean algebra is used to negate an expression or value. Applying this operator to true would return false and applying to false would return true. This property can be used to toggle a boolean value. The NOT operator is used before the variable to be switched and the result is assigned to the same variable. This effectively toggles a boolean value.

 

let bool = true;
bool = !bool;

console.log(bool); 

Tags:

Share:

Related posts