null: means no value;
undefined: represents an undeclared variable,
or a variable declared but not assigned a value,
Or an object property that doesn't exist.
The == operator treats the two as equal. If you want to distinguish between the two, use the === or typeof operator.
————————————-The dividing line of obscurity——————————————-
In JavaScript, null and undefined were once confusing. The following analysis will help you understand it more clearly (or make you more confused):
- null is a keyword; undefined is a property of the Global object
- null is an object (empty object, without any properties and methods); undefined Is a value of undefined type. Try the following code:
document.writeln(typeof null); //return object
document.writeln(typeof undefined); //return undefined- In the object model, all objects are instances of Object or its subclasses, with the exception of null objects:
document.writeln(null instanceof Object); //return false- null "Equal value (==)" to undefined, but not "congruent value (===)" to undefined:
document.writeln(null == undefined); //return true
document.writeln(null === undefined); //return false- Both null and undefined can be type-converted to false during operation, but are not equal to false:
document.writeln(!null, !undefined); //return true, true
document.writeln(null==false); //return false
document.writeln(undefined==false); //return false