34
var a = true;

How to get opposite value of a (false) and vice versa?

7 Answers 7

72
>>> a = true;
true
>>> !a;
false
Sign up to request clarification or add additional context in comments.

1 Comment

This works for a = false as well. I wasn't sure at first.
55

Use the logical NOT operator:

!a

3 Comments

lol im so sorry if this bumps the post but this answer and its comments couldn't go unrecognized.
@zetlen i laughed hard
I upvoted because you're right, and you stuck it to their validation!
14

This would be much easier

var b = !a

This would make true a false and false a true

Comments

4

Something like this, perhaps;

function toggleFlag(value){
   var toggle = value ? false : true;
   return toggle;
}

var a = true;

a = toggleFlag(a);

5 Comments

This is entirely too complicated for something this easy.
@JustinNiessner: Considering how basic the question is, I'd be inclined to upvote the most complicated answers provided. But that's just me.
Dunno, Justin... The principle is easy to follow, and extensible, no?
I like this answer. ! is obscure and unreadable; this is better, more maintainable code.
That's how I see it, but !a is far more elegant, tbh.
4

Try the following:

var a = !a;

Comments

3

Use the "not" operator

var a = true;
alert(!a); //=>false

Comments

-5

Try like Below

var Boolify = require('node-boolify').Boolify;
var opp_var = !Boolify(myVar);

Refer node-boolify

1 Comment

Bruh...........

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.