Null and undefined differences in JavaScript introduction to _javascript Tips

Source: Internet
Author: User

There are 2 special values in JavaScript that represent information that does not exist: null and undefined. The individual believes that the difference between these two special values can be understood from the following perspectives:

1.null represents a container for storing information (such as a variable that was previously assigned a value), but the contents of the container are empty.
2.undefined represents a container that does not exist for storing information.

Null in JavaScript is no different from the null in most other programming languages, basically to indicate that the information value is NULL, while in JavaScript the expression returns the result as undefined:

1. Variables that have never been assigned a value.
2. Access to a property value that does not exist for an object.
3. Access to a member that does not exist in the array.
4. Call a function that has no return statement.
5. Call return statement null ("return;") The function.

In fact, like Infinity and Nan, undefined is a global variable in JavaScript and can even be given other values in ECMAScript 3. ECMAScript 5 corrects the error and sets the undefined variable to read-only.

For comparisons between null and undefined, you can use the = = = Congruent operator. If you use the ordinary = = operator, null and undefined are equivalent:


Copy Code code as follows:

Console.log (null = = undefined);//true
Console.log (null = = undefined);//false


If you need to assign a null value to a variable during program writing, you typically use NULL instead of undefined. The reason for this is:

1.undefined is generally considered to be a system-level, error-level information missing.
2.null is generally considered to be a programming level, the logical operational level of information value is null.

If a type conversion is involved in a program, the result of null and undefined is not the same when converting to the number type:

The result of 1.undefined conversion to number is Nan.
The result of the 2.null conversion to number is 0.

It is worth mentioning that the result of converting an empty string and an empty array to number is also 0.

For JavaScript, why do you want to design two values that mean "no", see Nanyi's blog post.

Experiment

In the following experimental code, the result of an expression is undefined:

Copy Code code as follows:

var A;
Console.log (a);

function Sample (x) {
this.x = x;
}
var s = new Sample ();
Console.log (s.x)
Console.log (s.notexistvariable);

var n = [2,3,4];
Console.log (N[8]);

function Test () {
No return value to this function
}
Console.log (Test ());

function Test2 () {
Return
}
Console.log (Test2 ());

Related Article

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.