最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is the exact negation of if(variable) in javascript? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Apr 16, 2016 at 8:10 g.pickardou asked Apr 16, 2016 at 8:06 g.pickardoug.pickardou 36k43 gold badges152 silver badges318 bronze badges 4
  • What do you really want myVariable to be exactly? – StackSlave 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 – mplungjan Commented Apr 16, 2016 at 8:19
  • @PHPglue: I have no expectation for myVariable, the code does not modifies it. – g.pickardou Commented Apr 16, 2016 at 9:00
  • @mplungjan: That is not my aim at all. – g.pickardou Commented Apr 16, 2016 at 9:01
Add a ment  | 

6 Answers 6

Reset to default 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.

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 ment

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.

!(!!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!!

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

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

You can use it.

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

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.

发布评论

评论列表(0)

  1. 暂无评论