Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .
The IS NULL condition is satisfied if the column contains a null value or if the expression cannot be evaluated because it contains one or more null values. If you use the IS NOT NULL operator, the condition is satisfied when the operand is column value that is not null, or an expression that does not evaluate to null.
Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty.
In JavaScript, null is a special value that represents an empty or unknown value. For example, let number = null; The code above suggests that the number variable is empty at the moment and may have a value later.
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.
if (val !== null) { ... }
The Strict Not Version uses the "Strict Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6. The !==
has faster performance, than the !=
operator because the Strict Equality Comparison Algorithm doesn't typecast values.
if (val != 'null') { ... }
The Non-strict version uses the "Abstract Equality Comparison Algorithm" http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3. The !=
has slower performance, than the !==
operator because the Abstract Equality Comparison Algorithm typecasts values.
if (!!val) { ... }
The Double Not Version !!
has faster performance, than both the Strict Not Version !==
and the Non-Strict Not Version !=
(https://jsperf.com/tfm-not-null/6). However, it will typecast "Falsey" values like undefined
and NaN
into False (http://www.ecma-international.org/ecma-262/5.1/#sec-9.2) which may lead to unexpected results, and it has worse readability because null
isn't explicitly stated.
It's because val is not null
, but contains 'null'
as a string.
Try to check with 'null'
if ('null' != val)
For an explanation of when and why this works, see the details below.
Use !==
as !=
will get you into a world of nontransitive JavaScript truth table weirdness.
You should be using the strict not equals
comparison operator !==
so that if the user inputs "null"
then you won't get to the else
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With