Javascript no more than 0

How to check that the value is not greater than 0 in javascript?

I tried

 if(!a>0){} 

But it does not work.

+7
javascript jquery equals if-statement
source share
4 answers

You need a second set of brackets:

 if(!(a>0)){} 

Or, even better, "no more" is the same as saying "less than or equal to":

 if(a<=0){} 
+40
source share

Mureinik's answer is completely right, but seeing that “understanding false values” is one of the most important, less intuitive parts of JavaScript may be worth a little more explanation.

Without a second set of brackets, a statement that needs to be evaluated

 !a>0 

actually rated as

 (!a) > 0 

So what does (! A) mean? This means finding the logical truth "a" and flipping it; true becomes false and false becomes true. The logical likelihood of "a" means - if a has one of the meanings that is considered "false", then it is false. In all other cases, i.e. For all other possible meanings of a, this is true. False values:

 false 0 (and -0) "" (the empty string) null undefined NaN (Not a Number - a value which looks like a number, but cannot be evaluated as one 

So, if a has any of these meanings, it is false and! a true If he has something else, it is true, and therefore! A is false.

And then we try to compare this with 0. And 0, as you know, can also be false, so your comparison is

 if (true > false) {} 

or

 if (false > false) {} 

Seeing that neither truth nor falsehood can ever be anything other than false (they cannot be more or less!), Your "if" will always fail, and the code inside the brackets will never be evaluated.

+6
source share

a <= 0 or (less clearly IMO) !(a > 0)

The operator ! applies to a , not to the whole expression, so extra brackets are needed if you send a non-route.

+2
source share

If you're damn configured to use the > character, just cancel the operations

 if(0>a) 

If a can also be 0 , then

 if(0>=a) 
0
source share

All Articles