When typeof is executed on declared but uninitialized and undeclared variables, undefined is returned. null indicates a null object Pointer. The typeof operation will return the object. First, the difference between null and undefined is described as follows:
If typeof is executed on declared but uninitialized and undeclared variables, "undefined" is returned ".
Null indicates a null object Pointer. The typeof operation returns "object ".
Generally, the value of a variable is not explicitly set to undefined, but the opposite is null. For a variable that will save the object, the variable should be explicitly set to save the null value.
var bj;alert(bj); //"undefined"bj = null;alert(typeof bj); //"object"alert(bj == null); //truebj = {};alert(bj == null); //false
The following two functions are provided by brother Deng. Thank you.
/** Check whether the object is a null object (excluding any readable attributes ). * This method detects both attributes of an object and attributes inherited from the prototype (hasOwnProperty is not used ). */Function isEmpty (obj) {for (var name in obj) {return false;} return true ;};
Whether the empty object is {} or null. I wrote a test case.
Var a = {};. name = 'realwall'; console. log (isEmpty (a); // falseconsole. log (isEmpty ({}); // trueconsole. log (isEmpty (null); // true // when the parameter is null, no syntax error occurs. That is, although null pointer objects cannot be added, but can I use the for in statement? /** Check whether the object is a null object (excluding any readable attributes ). * The method only detects the attributes of the object and does not detect the attributes inherited from the prototype. */Function isOwnEmpty (obj) {for (var name in obj) {if (obj. hasOwnProperty (name) {return false ;}} return true ;};
Difference between {} and null:
This is very important.
Var a = {}; var B = null;. name = 'realwall'; B. name = 'Jim '; // an error is reported here. B is a null pointer object and cannot directly add attributes like a common object. B = a; B. name = 'Jim '; // At this time, a and B point to the same object. A. name and B. name are both 'jam'