So I have been through most of the questions here. Also quite a few articles good and bad.
One thing I am looking for some additional clarification on is how undefined and un declared variables are treated.
Take the code below.
var a;
if(a == null) // True - Due to Type Coercion
if(a == 'null') // False
if(a === null) // False
if(a === 'null') // False
if(a == undefined) // True
if(a === undefined) // True
if(a == 'undefined') // False
if(a === 'undefined') // False
if(a) // False - A is undefined
alert(typeof(a)) // undefined
All of the above I understand. But things get weird when you look at an undeclared variable. Note I am specifically omitting a "var b;".
alert(typeof(b)) // undefined
if(typeof(b) == 'undefined') // True
if(typeof(b) === 'undefined') // True - This tells me the function typeof is returning a string value
if(typeof(b) == 'null') // False
if(typeof(b) === 'null') // False
if(typeof(b) == null) // False
if(typeof(b) === null) // False
if(b) // Runtime Error - B is undefined
Any other operation then typeof(b) results in a runtime error. Still I can understand the logic behind the way the laguage is evaluating expressions.
So now I look at a non existent property of a and am really confused.
if(a.c) // Runtime Error - c is null or not an object
alert(typeof(a.c)) // Runtime Error - undefined is null or not an object
I would think that c in this case would be treated like b in the previous example but its not. You have to actually initialize a to something then you can get it to behave as b does. And stop it from throwing runtime errors.
Why is this the case? Is there some special handling of the undefined type or is the typeof function doing something recursively to evaluate the sub property that is throwing the runtime error?
I guess the practical question here is if I am checking a nested object c in a.c I can immediately assume c is undefined if a is undefined?
And what is the best way then if I wanted to check some extremely nested object to see if it was set like x in MyObject.Something.Something.Something.x ? I have to navigate through the structure element by element making sure each one exists before going to the next one down in the chain?