This question already has answers here:
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 6 years ago.
Sorry for what I'm sure had have been asked in the past, but it's very hard to search for a question like this. "!=" and "!==" are not exactly search friendly. If anyone knows a duplicate question you can point me to it.
What is the difference between doing myVar != null and myVar !== null?
I know that != is not equal and !== is not equal value or not equal type, but when comparing to null is there ever a case where they would return different results? Is one better to use than the other?
The answer to the specific question about whether there's ever a case where != and !== comparisons involving null get different answers is yes:
undefined != null // false
undefined == null // true
undefined === null // false
undefined !== null // true
The rules for == and != explicitly include a clause that stipulates that null and undefined are the same.
Personally — that is, in my code — that fact is a reason for using != (or ==) when checking for null in cases where undefined should be treated the same way (which is a pretty common situation).
Related
This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.
This question already has answers here:
How can I determine if a variable is 'undefined' or 'null'?
(34 answers)
Closed last month.
Is it possible to check for nullish values as part of an if block, without resorting to a pair of explicit checks for null and undefined.
For example, don't want to do this:
if(key === null || key === undefined) {
// do something
}
This doesn't work, because it also includes other falsy values such as zero and empty array.
if(!key)
That's one of the good use cases of the operator ==:
if(key == null) {
// do something
}
This will work with both null and undefined, while keeping away other falsy values.
This question already has answers here:
Detecting an undefined object property
(50 answers)
Closed 3 years ago.
I had been checking for undefined values myself like:
if(variable !== 'undefined')
then I came across the following code:
if(typeof variable1 !== 'undefined' || typeof variable2 !== 'undefined')
I followed the same convention knowing it's safer when variable goes undeclared. While in my code reviews, it was pointed I could simple write:
if(variable1 || variable2)
Which of these is the most standard way to check undefined?
There are various use case as following
if (variable) is standard way to check truthiness of any variable in javascript. You can examples of what values will be truthy on Truthy | MDN Web Docs. Also, Falsy | MDN Docs
The cases where you explicitly would check for undefined is when a variable has been declared but not assigned value or explicitly assigned undefined.
In that case use if (variable !== undefined).
If you are receiving response from an API which might consist of stringified value of undefined, which you are sure of, then only do the check if (variable !== 'undefined')
This question already has answers here:
How can I check for "undefined" in JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
Found this in our code. What's the difference between:
if (typeof isValid === 'undefined') {
and
if (isValid === 'undefined') {
Why would anyone use the first one, I don't understand how this makes sense?
This:
if (typeof isValid === 'undefined') {
checks to see if the type of isValid is "undefined". It could be "undefined" because either A) isValid is a variable with the value undefined in it, or B) It's an undeclared identifier.
This:
if (isValid === 'undefined') {
checks to see if the variable isValid contains the string "undefined". The variable must exist (e.g., be declared), or a ReferenceError is thrown.
You see the first in situations where the author isn't sure the variable isValid has been declared, or because they're worried that undefined may have been redefined in the scope where the code occurs, or because a long time ago they were worried that the undefined in one realm (loosely: window/tab) and the undefined in another realm wouldn't be === to each other. (If that was ever true, it hasn't been for at least a decade.)
if (typeof isValid === 'undefined') { means that you want to check if the variable has any value or not, whereas if (isValid === 'undefined') { means that you want to check if the variable has a string value 'undefined'
This question already has answers here:
variable === undefined vs. typeof variable === "undefined"
(9 answers)
Closed 5 years ago.
I have been using typeof operator to check if a variable is not defined like this:
if( typeof numLines === "undefined" ){
// do something
}
But the same can be achieved using:
if( numLines === undefined ){
// do something
}
As far as I remember, I had read somewhere that the typeof approach is better but now I think why should I use a slightly longer statement if there is no benefit at all in that. So my questions are:
Is there really any benefit in sticking to typeof approach for such checks?
Can you provide some examples where one approach is better than the other?
Thanks.
According to a related question undefined is not assured to be the thing you expect it to be:
var undefined = 'uh oh'
if (numLines === undefined) {
// Seems safe but isn't
}
else if (typeof(numlines) == 'undefined') {
// Actually gets it.
}
It's very odd that undefined is not nailed down in JavaScript, but that's how it is with the ECMA standard.