The last note said undefined, this note says null
Where does null come from?
Null, like undefined, is the default (unique) value for the simple data type NULL.
Remember that assigning undefined to a variable is window.undefined? Undefined is an attribute of window.
Null, on the other hand, is a literal. From a logical point of view, null represents an empty object pointer indicating that the variable is pointing to any object. Null is better understood as an object that has not yet been created.
When do I get null
Here’s an example:
<div id="app"></div> <script> let obj = document.getElementById('app'); // The getElementById() method returns a reference to console.log('obj',obj) for the first object with the specified ID; let obj2 = document.getElementById("container"); console.log('obj2',obj2) //obj2 null </script>Copy the code
The getElementById() method returns a reference to the first object with the specified ID. But the CONTAINER id does not exist, so null is returned.
typeof null
Null is the only default value for datatype NULL. Using the typeof operator to detect null will result in null.
Of course not:
On the one hand, it says that a null value represents an empty object. The pointer is actually an empty object. Typeof is detected as object.
On the other hand: In the original implementation of JavaScript, values in JavaScript were represented by a tag representing a type and actual data values. The type label of the object is 0. Since null represents a null pointer, the type label of null is also 0. Typeof NULL returns “object” in error. In ES6, it was proposed to correct the typeof NULL to null, but the proposal was rejected so the “object” type was kept.
How to null determine
We can no longer safely determine whether a value is null using the Typeof operator like undefined.
But you can use: use the strict equality match or unequal operator! = = judgment
let obj3 = null;
console.log(typeof obj3) //object
console.log(obj3 === null) //true
Copy the code
The use of the null
Under what circumstances do we assign a variable to NULL?
In the first case, it is best to initialize the variable to NULL if it will be used to hold objects in the future.
This has the advantage of helping to further distinguish null from undefined
Second case: dereference – When a data is no longer needed, we can set the value to NULL to release the reference.
Dereferencing does not automatically reclaim the memory occupied by the value; what it does is take the value out of the execution environment so that the garbage collector can reclaim it the next time it runs, and it helps eliminate the possibility of circular references. Dereference usually applies to most global variables and properties of global objects. Local variables are also automatically dereferenced when they leave the execution environment.
Undefined and null
In summary, let’s go back to the title of the article to summarize. Before summarizing, print undefined == null.
In fact, undefined is derived from the null value, so ECMA-262 requires that the equality test return true.
The standard equality operator == is used, which for the purpose of converting its operands to the same type for comparison;
Strict equality does not convert, whereas undefined and null are of a different type and therefore are not equal.
Undefined is used to fill null pits
The original version of JavaScript made this distinction: NULL is an object representing "nothing" (a null object pointer) that is zero when converted to a value; Undefined is a primitive value for "none", which is NaN when converted to a value.
I’m starting to think a little bit now that I’m asking myself the difference between undefined and null, and hopefully I’ll be able to remember something later.
Thank you cavszhouyou. Top/JavaScript %… The author’s summary, thank you for the screenshot of the front end Zhang.