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

g.pickardou
  • 32,346
  • 36
  • 123
  • 268

6 Answers6

4

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

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

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

Ori Drori
  • 183,571
  • 29
  • 224
  • 209
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.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 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 Apr 16 '16 at 09:04
  • @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. http://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript – gurvinder372 Apr 16 '16 at 09:24
  • 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 Apr 16 '16 at 09:31
  • @g.pickardou true, it will first coerce the expression into a boolean one as per the table given in spec and then negate it. – gurvinder372 Apr 16 '16 at 09:33
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!!

maioman
  • 18,154
  • 4
  • 36
  • 42
0

You can use it.

if (typeof myVariable !="undefined" && myVariable !="" && myVariable !=null) {
    // your code
} else {
    // your code
}
MD. ABDUL Halim
  • 712
  • 3
  • 11
  • 32
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.

  • do you mean typeof myVariable !== undefined ? – g.pickardou Apr 16 '16 at 08:14
  • 1
    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. – Amandeep Punia Apr 16 '16 at 08:34
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.