1

Context:

I often see in javascript programs the following conditional statement:

if (myVariable){
....
}

As I understand do fat it tests against both undefined and null but I an not sure what else, I suppose against boolean false...

Theoretically if I want to write the exact negation I can wrote

if (myVariable){
   // dummy empty
}
else{
   // My conditional code here
}

Question

What is the exact equivalent of the code above in the following simplest form?

if (<what to write here using myVariable>){
     // My conditional code here
}

The answer if (!myVariable) seems too obvious, and I am not sure there are no traps here regarding undefined, null behaviours...

4
  • What do you really want myVariable to be exactly? Commented Apr 16, 2016 at 8:16
  • If your aim is to have a one liner to not continue if the var is truthy, you can in a function write if (myvar) return; // your code after this Commented Apr 16, 2016 at 8:19
  • @PHPglue: I have no expectation for myVariable, the code does not modifies it. Commented Apr 16, 2016 at 9:00
  • @mplungjan: That is not my aim at all. Commented Apr 16, 2016 at 9:01

6 Answers 6

5

! negates a statement so !false === true:

if (!myVariable){
   // My conditional code here
}

It actually negates all non truthy values, so !null === true as well.

Sign up to request clarification or add additional context in comments.

Comments

4

try

if (!Boolean(myVariable))
{  
  //logic when myVariable is not 0, -0, null, undefined, false,  
}

check this spec and this

  1. Let exprValue be ToBoolean(GetValue(exprRef)).

So, conversion of Boolean is the first thing that happens to an expression.

You can apply the same thing to an individual variable as well.

Edit:

To explain the question in comment

how the "!myVariabla" expression is evaluating

It will first coerce the expression into a boolean one as per the table given in spec and then negate it.

4 Comments

Thanks for the references. I am wondering whether "!myVariable" is equivalent with "!Boolean(myVariable)" or not. If yes, then "if (myVariable)" will do it. If not then then "if (!Boolean(myVarable))" must be used
@g.pickardou Just tested almost all falsey values, they give same results with !myvar and !Boolean(myvar). I guess that is because when just a single variable is put inside if, it is automatically coerced into a boolean value. stackoverflow.com/questions/19839952/…
Many thanks for the further information. However the question in the comment already has nothing to do with the "if" statement specification, it is about the "!" operator specification. The is specification (what you referred) say that "if (!myVariable)" will be evaluated as "if (Boolean(!myVariable))". Now we need to know how the "!myVariabla" expression is evaluating. Anyway, your answer is clearly does the job regardless how "!myVariable" is evaluated.. (because your answer workarounds that)
@g.pickardou true, it will first coerce the expression into a boolean one as per the table given in spec and then negate it.
1
!(!!myvar)

the negation of its Boolean() value;


In real life using !myvar would be enough, but for example:

console.log(NaN !== NaN)

yields different results from:

console.log(!!NaN !== !!NaN)

as long as you know how types are coerced you're safe!!

Comments

0
if (typeof myVariable===undefined|| !myVariable){
     // My conditional code here
}

It should do, it will run for undefined as well as null & negation of myVariable.

2 Comments

do you mean typeof myVariable !== undefined ?
No, as per the question statement, the condition should work in all cases which are else condition of (whenever myVariable is true), so when myVariable is undefined also it is not true..so the block should execute. Thus i have kept === undefined.
0

You can use it.

if (typeof myVariable !="undefined" && myVariable !="" && myVariable !=null) {
    // your code
} else {
    // your code
}

Comments

0

myVariable is true for: boolean true, any non empty string, any non zero numeric, any array (including an empty array), any object (including an empty object)

myVariable is false for null and for undefined variables

Negation of any true case always gives false, negation of any false case always gives true.

Comments

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.