1null is a special object because typeof null returns 'object'.
2On the other hand,
3undefined means that the variable has not been declared,
4or has not been given a value.
1var myVar=null;
2
3if(myVar === null){
4 //I am null;
5}
6
7if (typeof myVar === 'undefined'){
8 //myVar is undefined
9}
10
1 console.log(null === null); // true
2 console.log(undefined === undefined); // true
3 console.log(null + undefined === null + undefined); // false
4
5// null + undfined equals NaN, and NaN isn't equal to itself :(
1/* In Javascript,
2* null: a value explicitly set by the programmer for the intentional lack of value.
3* undefined: a value implictily or explicitly set for the intentional lack of value.
4*/
5
6console.log(null == undefined) // true
7console.log(null === undefined) // false
8
9let a = null
10console.log(a) // null
11
12let c;
13let d = undefined;
14console.log(c, d) // undefined undefined
15
16console.log(typeof null) // Object
17console.log(typeof undefined) // undefined
1Undefined used for unintentionally missing values.
2
3Null used for intentionally missing values.
1//loosely equal (compare values between two variables)
2null == undefined // true ( null => 0 , undefined => NAN)
3
4//strictly not equal (compare both type and value)
5null === undefined // false (typeof null => object , typeof undefined => undefined)
6null !== undefined // true (typeof null => object , typeof undefined => undefined)
7