There are two special values in JavaScript: undefined and null, which need to be noticed when comparing them. The undefined value is obtained when reading an unassigned variable or attempting to read an attribute that is not in an object.
Output results:
Javascript also defines a special value of NULL, which is slightly different from undefined. The latter is a value that is not defined as worth, and the former is used to indicate that a value has been assigned but that the value is not a valid object, string, number, or Boolean value (that is, the definition is a value-free [no value]).
The following code uses undefined and null successively to show its different effects:
Output results:
var:undefined
prop:false
var:sunny
prop:true
var:null
prop:true
1. Check whether the variable or property is undefined or null
If you want to check whether a property is null or undefined (whichever), use the IF statement and the logical non-operator (!) only. Can.
Output results:
The name is not null or undefined the city is
null or undefined
weather is null or undefined
2. Distinguishing between null and undefined
When comparing two values, the method used should be determined as necessary. If you want to treat undefined values and null values equally, you should use the equality operator (= =) to have Javascript type cast. A variable with a value of undefined is considered equal to a variable with a value of NULL. If you want to differentiate between null and undefined, you should use the equivalent operator (= =).
Output results:
Equality:true
Identity:false
The above JavaScript basics _ simple comparison undefined and null value is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.