How to toggle a boolean?

后端 未结 6 1240
醉话见心
醉话见心 2020-12-07 07:14

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:<

6条回答
  •  [愿得一人]
    2020-12-07 07:47

    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.

提交回复
热议问题