They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null.
The only values that are not truthy in JavaScript are the following (a.k.a. falsy values):
nullundefined0""(the empty string)falseNaN
Hi, all, I often do stuff like this in my code, to check for a variable being not null and not undefined.
// check if value is not null and not undefined
if (value) {
...
}However, I'm now thinking this can leads to bugs, because of 0, "", false and NaN also being falsy.
What is a better way to check a variable is not null and not undefined? I could use this I think, wondering if there is something shorter than this:
if (typeof value !== 'undefined' || value !== null) {
...
}They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null.
The only values that are not truthy in JavaScript are the following (a.k.a. falsy values):
nullundefined0""(the empty string)falseNaN
Here is how you can test if a variable is not NULL:
if (myVar !== null) {...}
the block will be executed if myVar is not null.. it will be executed if myVar is undefined or false or 0 or NaN or anything else..
Videos
Is null false in JavaScript?
What is a null check?
What is a strict null check?
this will do the trick for you
if (!!val) {
alert("this is not null")
} else {
alert("this is null")
}
There are 3 ways to check for "not null". My recommendation is to use the Strict Not Version.
1. Strict Not Version
if (val !== null) { ... }
The Strict Not Version uses the Strict Equality Comparison Algorithm. The !== operator has faster performance than the != operator, because the Strict Equality Comparison Algorithm doesn't typecast values.
2. Non-strict Not Version
if (val != null) { ... }
The Non-strict Not Version uses the Abstract Equality Comparison Algorithm. The != operator has slower performance than the !== operator, because the Abstract Equality Comparison Algorithm typecasts values.
3. Double Not Version
if (!!val) { ... }
The Double Not Version has faster performance than both the Strict Not Version and the Non-Strict Not Version. However, the !! operator will typecast "falsey" values like 0, '', undefined and NaN into false, which may lead to unexpected results, and it has worse readability because null isn't explicitly stated.
If you truly want to confirm that a variable is not null and not an empty string specifically, you would write:
if(data !== null && data !== '') {
// do something
}
Notice that I changed your code to check for type equality (!==|===).
If, however you just want to make sure, that a code will run only for "reasonable" values, then you can, as others have stated already, write:
if (data) {
// do something
}
Since, in javascript, both null values, and empty strings, equals to false (i.e. null == false).
The difference between those 2 parts of code is that, for the first one, every value that is not specifically null or an empty string, will enter the if. But, on the second one, every true-ish value will enter the if: false, 0, null, undefined and empty strings, would not.
Instead of using
if(data !== null && data !== '' && data!==undefined) {
// do something
}
You can use below simple code
if(Boolean(value)){
// do something
}
- Values that are intuitively โemptyโ, like 0, an empty string, null, undefined, and NaN, become false
- Other values become true