null vs undefined vs undeclared
Null
- variable with
nullhas been explicitly assigned to a value which represents no value - check if a variable is
nullby using===operator
Undefined
- variable that is
undefinedhas been declared but not assigned a value - check if a variable is
undefinedby usingtypeofoperator or===operator
Undeclared
- variable is
undeclaredif it has not been declared usingvar,let, orconstkeywords, these variables will be declared globally but will throwReferenceErrorin strict mode - check if a variable is
undeclaredby usingtryandcatchblock
Never leave variables undeclared or unassigned
Never use == operator to check for null or undefined
console.log(null == undefined); // true