How to toggle a boolean?

Chris Dutrow picture Chris Dutrow · Jul 23, 2012 · Viewed 275.1k times · Source

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;

Answer

Jordan picture Jordan · Jul 23, 2012
bool = !bool;

This holds true in most languages.