Historical issues
When js was invented in 1995, other languages like c++ had null instead of undefined, and null to indicate that objects (referencing data types) had no value. Js is divided into primitive data types and complex data types (reference data types). The creators of JS thought it necessary to use another word to express the nought of the original value. That’s why undefined was invented.
undefined
- Undefined indicates the initial value of the original value. Undefined means undefined
- Undefined occurs in the following cases:
- The variable is declared, but not assigned. This variable is undefined
- Object property not assigned this property is undefined
- Arguments that should be provided by the calling function are provided but not provided. This parameter is undefined
- The function has no return value. Returns undefined after execution
- Viod () creates undefined
- In order to distinguish null from undefined in history.
Number(undefined)
Returns NaN - In IE8, undefined is not a keyword and is allowed
var undefined = 1; console.log(undefined); / / 1
That’s how it works. In the current browser, the above code also allows, but the content is undefined;That is, the current browser undefined will return undefined when assigned as a variable namevar undefined = 1; console.log(undefined); // undefined
null
- Null represents the initial value of a complex data type, also known as the initial value of an object. Null means a reference to an empty object, so
typeof null // "object"
. - Null is the end of the prototype chain
Object.getPrototypeOf(Object.prototype) // null
- As an argument to a function, indicating that the argument to the function is not an object.