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')
Related
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:
What is the purpose of passing-in undefined?
(3 answers)
Closed 5 years ago.
I have just come across a function which accepts a parameter actually called 'undefined'. It is something like this:
(function($,undefined) {
// Do something
})(jQuery);
Either I'm going crazy, or there is no logical reason for this to be here as, well, undefined is undefined is undefined. Please can someone confirm either way? Thanks.
That is a classic trick to have an undefined variable to check against, typically:
if (someVar === undefined) {}
// instead of:
if (typeof someVar === 'undefined') {}
Notice that the wrapper IIFE does not pass any second argument, making the undefined parameter effectively undefined.
This question already has answers here:
Null-safe property access (and conditional assignment) in ES6/2015
(11 answers)
JS checking deep object property existence [duplicate]
(2 answers)
Closed 6 years ago.
I dont know if a.b is set. I want to do something only if a.b[0].c is true. I can do something like this:
if (a.b && a.b[0] && a.b[0].c) {
// ...
}
Is there a shortcut to check nested existence? Can I simplify this kind of condition?
I used to code golf, and one of the tricks we used is the following.
(myArray || [])[0] || 0
First, myArray || [] is evaluated, if myArray exists, this returns it, if it doesn't, then it returns an empty array. Let's say myArray isn't defined. Next, the [][0] || 0 expression gets evaluated ans because [][0] is undefined, this returns 0.
In your case it could be used the following way:
((a.b || [])[0] || {}).c || {}
This returns an empty object if something's undefined.
I'm not saying you should use this (in fact, you shouldn't), I just want to show you, that there is a smaller solution.
Update:
If tc39 gets through, then you'll have a much better solution using optional chaining:
a.b?[0]?.c
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).
This question already has answers here:
Why is typeof null "object"?
(11 answers)
Closed 7 years ago.
I have found a strange behavior for the type operator return value. Take a look at the following code:
So I obtain object as the type of null using typeof operator. Is that a browser related bug or is it the expected behaviour? It does not sound normal to me that typeof would return "object" for a null value. I would expect null instead. I have tested on chrome. Thanks
According to MDN this is a bug in ECMAScript, should be null.
In the first implementation of JavaScript, JavaScript values were
represented as a type tag and a value. The type tag for objects was 0.
null was represented as the NULL pointer (0x00 in most platforms).
Consequently, null had 0 as type tag, hence the bogus typeof return
value. (
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
This is the expected behavior, as defined in the ES5 spec at section 11.4.3:
Return a String determined by Type(val) according to Table 20.
...
Type of val Result
Null "object"
The typeof operator is never defined to return 'null' for any value, regardless of whether it's actually null or not. It does handle undefined as you might expect, but they chose not to treat null specially.