From the general answer:
In fact, in the original type of ECMAScript, there are undefined and Null types. Both of these types correspond to their own unique private values, namely undefined and null.
The value undefined is actually derived from the value null, so ECMAScript defines them as equal, which can be verified by the following code:
alert (undefined = = null); True
Although the two values are equal, they have different meanings.
A undefined is a value that is assigned to a variable when it is declared but not initialized, and null is used to represent an object that does not already exist. If the function or method is to return an object, the object returned is usually null when it is not found.
So alert (undefined===null);//false
To tell the truth, I did not see why undefined will inherit null, that is, why Undefined!==null, and the uninitialized variables and functions returned by the object does not exist between the difference between, the problem of various, people are very unconvincing.
Look at how the memory is said:
The udefined represents a basic data type that does not have a value assigned.
Null represents a reference data type that is not assigned a value.
Let's look at a piece of code:
12345678 |
var age;
var id = 100;
var div02 = document.getElementById(
"div02"
);
//注:div02是不存在的
var div01 = document.getElementById(
"div01"
);
//注:div01存在
alert(id);
//100
alert(age);
//undefined
alert(div02);
//null
alert(div01);
//object
|
Let's take a look at the memory situation:
Solve the first question: Why Undefine inherits from null
In JavaScript, the base data type has a reference data type corresponding to it, number Number,string String,boolean Boolean ..., they have exactly the same behavior, and each other produces automatic unboxing and boxing operations. The meaning of the basic data types in the stack memory is described in the memory analysis article, which leads to a superficial conclusion: the basic data type is a subclass of the corresponding reference data type, but only for the purpose of increasing the efficiency and placing it in the stack memory, the corresponding undefined represents the basic type of no value. Null represents a reference type that has no value, and it is bound to eject undefined to inherit null.
Solve the second question: why Undefined==null
Come up with the answer undefined inherit from null, memory tells us the answer they're all in the stack
Solve the third problem: why Undefined!==null
Memory tells us that their meaning is really not the same, the old saying: udefined represents a basic data type that does not have an assignment, and null represents a reference data type that has no assigned value. There's a big difference in their memory map.
Workaround for additional issues: null is handling references, why NULL is in stack memory, not in heap memory
The answer is as simple as the efficiency! It is necessary to allocate an extra piece of memory in the stack to point to NULL in the heap!
Extra Harvest:
When we want to cut off the connection to the object, but do not want to assign the variable to the other value, then we can place null, such as var obj = new Object (); obj=null;
Some behavior about undefined and null
When NULL participates in numeric operations, its value is automatically converted to 0, so the following expressions are evaluated to get the correct values:
Expression: 123 + null result value: 123
typeof Null returns an Object because null represents a reference that is not a value.
Undefined is a special property of a Global object (window) whose value is a private value of type undefined undefined
When undefined participates in any numerical calculation, the result must be Nan.
When a declared variable is not initialized, the default value of the variable is undefined, but undefined differs from the undefined value. The typeof operator cannot differentiate between these two values
So the judgment of whether a variable exists is judged by the if (typeof var = = ' undefined ') {//code here}, so that it is fully compatible with undefined (undefined) and uninitialized (uninitialized) two cases
Haha, when you stand in the memory of the height of the analysis of the problem, so abstract things have a real performance, everything becomes simple!
How to tell if a variable exists, when
(1) Variable declaration not defined
(2) Variable for declaration
(3) Variable declaration and assigning it a value of undefined (not the string "undefined")
Undefined and parameter judgment
Let's look at a simple function:
123456 |
function bool(val){
if
(
typeof val ==
‘undefined‘
){
return true
;
}
return !!val;
}
|
Is there a problem with this function? Yes, because typeof return undefined value there are two possible, one is passed in is undefined, there is no value.
12 |
alert(bool(undefined)); //true alert(bool()); //true |
Obviously, bool (undefined) returns the false that is not what we expected. How to solve this problem?
123456 |
< Code class= "javascript keyword" >function bool (val) { &NBSP;&NBSP;&NBSP;&NBSP; if (arguments.length!==0) { return !! Val; &NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; return true ; } |
We determine whether the parameters are passed by the length of the arguments parameter, so that the parameter passed by the function is undefined, or there is no pass value at all!
Understanding javascript__ Understanding undefined and null