undefined - something has’t been initialized
null - something is currently unavailable/absent
null and undefined are equal to each other when using ==
/ Both null and undefined are only `==` to themselves and each other:
console.log(null == null); // true (of course)
console.log(undefined == undefined); // true (of course)
**console.log(null == undefined); // true**
but you don’t need to worry about falsy values making through the check
console.log(0 == undefined); // false
console.log('' == undefined); // false
console.log(false == undefined); // false
Recommand “== null” to check both null and undefined
a == null
This suits both null and undefined check
Don’t use “== null” check for root level things in strict mode
To check if a variable is defined or not at a global level, use typeof
if (typeof someglobal !== 'undefined') {
// someglobal is now safe to use
console.log(someglobal);
}
Because TypeScript gives you the opportunity to document your structures separately from values instead of stuff like:
function foo(){
// if Something
return {a:1,b:2};
// else
return {a:1,b:undefined};
}