翻转布尔变量的简写

Chi*_*nke 27 javascript boolean flip shorthand

如何在javascript中翻转布尔变量的值,而不必两次包含变量名?所以

foobarthings[foothing][barthing] = !foobarthings[foothing][barthing];
Run Code Online (Sandbox Code Playgroud)

没有写foobarthings[foothing][barthing]两次.

ale*_*lex 28

没有比你现在更短的方式了.

  • @JacobSchneider 您发布的代码在语法上无效,并且 OP 已经演示了“!”运算符的知识。 (2认同)

Sjo*_*erd 8

你可以这样做:

foo ^= 1
Run Code Online (Sandbox Code Playgroud)

但这真的会在0和1之间切换foo,而不是真和假.