Undeclared vs Null
null is both an Object "type" and one of the 7 unique primitive value types called null
undefined is both a global scope property called undefined (window.undefined) and one of the 7 unique primitive value types called undefined
It is the primitive types we use as values we are interested in.
In the case of null, as a value type it means an empty value has been assigned to a variable. It may mean a variable has an empty value but it is still a value. It also initializes the variable so it exists, and is not undefined.
undefined is a special case. When you create a variable it is assigned undefined by default prior to assigning a value, and implies the variable does not exist or exists but has no value assigned. Like null it is also a primitive value type. But unlike null it means the variable does not exist. That is why its always better to check if the variable exists and has been assigned a variable using undefined before checking if the value is null or empty. undefined implies no variable or object exists in the compilation. The variable has either not been declared or declared with a missing value so not initialized. So undefined is a very good way to avoid many types of errors and supersedes null.
That is why I would not rely on "truthy" checks for true/false with null and undefined, even though they will return a false response, as undefined implies a missing feature, object, or variable, not just a true/false check. It implies something more.
Let's look at undefined first:
//var check1;// variable doesnt even exist so not assigned to "undefined"
var check2;// variable declared but not initialized so assigned "undefined"
var check3 = 'hello world';// variable has a value so not undefined
console.log('What is undefined?');
//console.log(check1 === undefined);// ERROR! check1 does not exist yet so not assigned undefined!
console.log(check2 === undefined);// True
console.log(check3 === undefined);// False
console.log(typeof check1 === 'undefined');// True
console.log(typeof check2 === 'undefined');// True
console.log(typeof check3 === 'undefined');// False
As you can see undeclared variables, or declared but not initialized, both are assigned a type of undefined. Notice declared variables that are not initialized are assigned a value of undefined, the primitive value type but variables that do not exist are undefined types.
null has nothing to do with missing variables or variables not yet assigned values, as null is still a value. So anything with a null is already declared and initialized. Also notice a variable assigned a null value is actually an object type unlike undefined types. For example...
var check4 = null;
var check5 = 'hello world';
console.log('What is null?');
console.log(check4 === undefined);// False
console.log(check5 === undefined);// False
console.log(typeof check4 === 'undefined');// False
console.log(typeof check5 === 'undefined');// False
console.log(typeof check4);// return 'object'
console.log(typeof check5);// return 'string'
As you can see each act differently and yet both are primitive values you can assign any variable. Just understand they represent different states of variables and objects.