null and undefined in JS

Source: Internet
Author: User

JS in the 6 major types: Number,string,boolean,null,undefined,object, where null and undefined accounted for 2 seats, and their role is to represent the empty, In my usual work I have not found the difference between the two, then what is the difference between them?

It is mainly embodied in semantics:

NULL indicates that a value is defined and defined as a null value. Undefined indicates that the value is not defined at all and the value does not exist . In other words, setting a value of NULL is reasonable, and setting a value of undefined is unreasonable.

Note whether the value is null or undefined, the variable must be declared, otherwise it will be an error, which is different from the value of the Access property.

var A; var b == = = = = = undefined);    // true null);    // false Console.log (c);     // Error  = = = undefined);    // true null);    // false

When the property is accessed, the internal [[get]] method is called (see Uncle Tom's deep understanding of the JavaScript series), which returns undefined if no attributes are found on the prototype chain.

O.[[get]] (P)://if it is a property of its own, it returnsif(O.hasownproperty (P)) {returnO.P;} //Otherwise, continue to analyze the prototypevar__proto =O.[[prototype]];//If the prototype is null, return undefined//This is possible: top-level Object.prototype. [[[Prototype]] is nullif(__proto = = =NULL) {  returnundefined;} //Otherwise, recursive invocation of the prototype chain [[Get]], find properties in the prototypes of each layer//until the prototype is nullreturn__proto. [[Get]] P

Back to our topic, so if you determine whether a value exists, then you should use obj.value = = = undefined without applying null.

In the usual cases:

1. The end point of the prototype chain is NULL

2. The variable declaration is not assigned a value of undefined

3. The attribute value that the object does not define is undefined

4. Returns undefined when the function has no return value

5. When calling a function, the missing parameter value is undefined

There are shortcomings, welcome to correct the exchange.

null and undefined in

JS

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.