The difference between null and undefined

Source: Internet
Author: User

Null: indicates no value;

Undefined: Represents an undeclared variable, or a variable that has been declared but not assigned a value, or an object property that does not exist. The = = operator considers the two equal. If you want to differentiate between the two, use the = = = or typeof operator.

Using the IF (!object) {} both contain

Add: 2006.12.6www.120hrb.com

var obj = "AAA";

var nullobj;

if (obj = = NULL | | obj = = Undefined | | (!obj)) {

Alert ("obj is null");

}

if (nullobj = = null) {

Alert ("obj is null");

}

if (nullobj = = undefined) {

Alert ("obj is undefined");

}

if (!nullobj) {

Alert ("! Obj ");

}

Supplement to undefined and "undefined (2007/1/30):

What does it mean for undefined undefined identifiers in JScript to indicate what is undefined? What is the difference and connection between it and "undefined" (including ")? Why is it possible to use undefined to compare variables and sometimes not?

The difference between underfined and "undefined" can be seen at a glance. In general, we think that undefined is a "keyword" provided by JScript, while "undefined" is a string that is not in suspense, but that the content in the quotation marks is as long as undefined. Although the difference between undefined and "undefined" is very obvious, they are closely related.

Read the JScript manual carefully, in fact, this underfined is a "defined" global value, not what its literal meaning is expressed as undefined. Let's look at the following code example, which is interesting:

The result of the execution is: www.hrbfkyy120.com

Let's change the above code a little bit and add a typeof to see:

What should be the result? Show "Object" and "undefined"? Of course not, the two alert calls will show "undefined".

So undefined is a constant defined by the scripting engine that exists after the scripting engine is initialized. Its actual function is to represent a defined variable of the initialization state (uninitialized), such as Var i, then the value of this I is undefined, and I is a real define, but not initialized. At this point we can write the expression to judge I, such as if (i = = undefined). If a variable that has never been in the code is used, then the undefined concept is not undefined the described uninitialized, but rather indicates that the variable is not fully enlisted in the context of the scripting engine. Using a statement like if (abc = = undefined), you get a second error message similar to the one in the.

In practice, if you use TypeOf to determine if a variable is undefined, it can be fully compatible with both undefined (undefined) and uninitialized (uninitialized) cases, but mostly I don't like to use if (typeof xxx = = ' undefined Because the literal string is prone to spelling errors, it is not professional in terms of the strong-typed language that is used to it.

Undefined: Undefined such as declaring a variable without assigning it a value, or using a nonexistent object property www.mlybyby.com

Null: null This is really not a good idea.

As if to be compatible with the previous browser (IE4.0 only undefined bar) and the null and undefined think is the same

The difference between null and undefined

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.