- Undefined
- Null
1, Undefined
1. Define a variable but do not initialize it, for example var a; 2. When a function is called and the number of arguments is smaller than the number of parameters, the parameter is undefined. 3. Undefined is returned when calling an attribute that has not been added to an object.
var obj={} console.log(obj.name); //undefinedCopy the code
4. Undefined is returned when a function is called with no return value.
function Person(name,age){ this.name=name; this.age=age; } var p=Person(" Person ",23); // p=undefined;Copy the code
5. Typeof returns undefined for uninitialized variables; 6. Typeof on undeclared variables also returns undefined.
var message; // Uninitialized variable alert(typeof message); // uninitialized variable alert(typeof message); // "undefined" // undeclared variable alert(typeof age); // "undefined"Copy the code
2. Null type
1. Manually set the value of a variable or an attribute of an object to NULL (when initializing the object, manually set the object to NULL. When an object is no longer needed in scope, null is assigned to the variable to dereference it, freeing memory.
var d=document.getElementById("d"); console.log(d); // Return null if there is no label with id "d"Copy the code
Object.prototype._proto_ is also null. (Each Object has a __proto__ attribute that points to the prototype attribute of the corresponding constructor, but since Object is the base class of all classes, it has no corresponding constructor and all object.prototype. _proto_ values are null);
console.log("a".__proto__); // Pointing to the prototype attribute of String //String {"", length: 0, constructor: ƒ, anchor: ƒ, big: ƒ, blink: ƒ... }Copy the code
4. If no result is captured, the default value is null.