Shorthand for flipping a boolean variable
18,330
Solution 1
There is no shorter way than what you currently have.
Solution 2
You can do this:
foo ^= 1
But this really switches foo between 0 and 1, not true and false.
Solution 3
var value = true;
alert(value);
value ^= true;
alert(value);
You could get 1 or 0 here
Author by
chtenb
Updated on June 06, 2022Comments
-
chtenb about 2 years
How can I flip the value of a boolean variable in javascript, without having to include the variable name twice? So
foobarthings[foothing][barthing] = !foobarthings[foothing][barthing];
without writing
foobarthings[foothing][barthing]
twice.