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