Null = = undefined? __javascript

Source: Internet
Author: User
Tags object object

Reproduced from:

Author: one pixel

Www.cnblogs.com/onepixel/p/7337248.html


In a recent book on "JavaScript Advanced Programming", which mentions the equality operator (= =), you cannot convert null and undefined to any other value until you compare equality, but remember that null = = undefined returns true. Yes, it is also defined in the ECMAScript specification, but I think it seems to be a little superficial and there are a lot of articles on the web, and I want to look at the difference between null and undefined from a whole new perspective to understand why the two are equal:


Undefined and Null are two special types of primitive data types (Primary type) in Javascript that have only one value, corresponding to Undefined and null, each of which has different semantics and scenes, But they show a similar behavior:


1, undefined

The literal meaning of a undefined is an undefined value, the semantics of which is to represent the most primitive state of a variable, rather than the result of an artificial action. This primitive state can occur in the following 4 scenarios:


"1" declares a variable, but has no assigned value

var foo;
Console.log (foo); Undefined
Access Foo, which returns undefined, indicates that the variable has never been used since it was declared, and that no valid values have been defined, that is, in an original and unusable state.


"2" accesses a property that does not exist on the object

Console.log (Object.foo); Undefined

Accesses the Foo property on the Object object, and also returns undefined, which indicates that no attribute named "foo" exists on object or has no definition.


The "3" function defines a formal parameter, but does not pass the argument

function defines the parameter a
function fn (a) {
    console.log (a);//undefined
}
fn ();//failed to pass argument
The function fn defines parameter A, but the FN is invoked without passing arguments, so the FN run-time argument A is an original, unassigned variable.

"4" uses void to evaluate an expression

void 0; Undefined
void false;//undefined
void [];//undefined
void null;//undefined
void function fn () {};/ /undefined
The ECMAScript specification void operator returns undefined for any expression evaluation, the function that does not return a value after performing an operation is the same, the function in JavaScript has a return value, and when no return operation is returned, it returns an original state value by default. This value is undefined, indicating that the return value of the function has not been defined.


Therefore, undefined generally comes from the most primitive state values of an expression, not as a result of human manipulation. Of course, you can also manually assign a value to a variable undefined, but this does not make sense, because a variable is not assigned is undefined.


2, NULL

The literal meaning of NULL is NULL, and the semantics of this value is to indicate that an object is artificially reset to an empty object rather than a variable's original state. The representation in memory is that the variables in the stack do not point to memory objects in the heap, namely:


When an object is assigned null, the original object is in the Free State in memory, and the GC timing the object and frees the memory. Therefore, if an object needs to be disposed, the variable is set to NULL, which means that the object has been emptied and is currently in an invalid state. Imagine if it would be awkward to replace null here with undefined? Obviously the semantic is not valid, its operation cannot express the behavior which it wants correctly.


Another problem related to null needs to be explained:

typeof null = = ' object '  
Null has its own type of NULL, not the type of object, and TypeOf is judged to be of type object because JavaScript data types are represented in binary form at the bottom, and the first three bits of binary binary 0 are judged by TypeOf as object types. The null bits happens to be 0, so null is erroneously judged to be of type Object.


000– object, data is an object of application

31-bit signed integer with data

010– double precision type, data is a double-precision number

100– string, data is a string

110– Boolean type, Data is Boolean


In fact, we can get the true type of NULL in another way:

Object.prototype.toString.call (NULL); [Object Null]

By using the ToString () method on the object prototype, you can obtain the true data type of the object in JavaScript, and of course the undefined type can be obtained in this way:
Object.prototype.toString.call (undefined); [Object Undefined]


3. Similarity

Although undefined and null have different semantics and scenarios, they all represent an invalid value, in summary. Therefore, when you access the property in JS, you get the result of the exception:


The ECMAScript specification argues that since null and undefined behave similarly, and all represent an invalid value, they represent a similarity to the

undefined = null; True

Do not attempt to interpret this conclusion by converting the data type, because:

Number (NULL); 0 number
(undefined);//NaN
 
//before comparing equality, NULL is not converted to another type
null = = 0;//false

however, = = = Returns false because the congruent operation = = = does not actively convert the data type of the sub-item when it is more equal, and the two do not belong to the same type:

undefined = = NULL; False, the type is not the same
undefined!== null;  True, the type is not the same


4, Summary

The difference between the two is that undefined represents a natural, primitive state value of a variable, while null indicates that a variable is artificially set to an empty object, rather than a primitive state. Therefore, in the actual use of the process, in order to ensure that the variables represented by the semantics, do not explicitly assign a variable undefined, when you need to release an object, the direct assignment is null.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.