Null and undefined in JavaScript

Source: Internet
Author: User
Javascript

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 regards both as equal. If you want to distinguish between the two, use the = = = or typeof operator.
Using the IF (!object) {} both contain the

Added: 2006.12.6
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 on undefined and "undefined" (2007/1/30):

    in JScript is used to denote undefined undefined identifiers exactly what is undefined? What is the difference between it and undefined to compare with variables, and sometimes not?

    underfined and "undefined" The difference can be seen at a glance. In general, we think that undefined is a "keyword" provided by JScript, and "undefined" But there is no suspense is a string, only the contents of the quotation marks and undefined . The difference between the undefined and is obvious, but they are closely related.

    Carefully read the JScript manual, which is actually a "defined" global value, rather than its literal meaning of undefined underfined. Let's look at the following code example, which is interesting:

<script language= "JavaScript" >
alert (undefined);
alert (variable);
</script>


The results of the execution are:


Let's modify the code slightly and add a typeof to see:

<script language= "JavaScript" >
alert (typeof undefined);
Alert (typeof variable);
</script>


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

So undefined is a constant defined by the scripting engine that exists after the script engine initializes. Its actual function is to represent the initialization state (uninitialized) of a defined variable, such as Var I, at which point the value of this I is undefined, and I is a real define, just uninitialized. Then 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, the undefined concept is not to undefined the description, but rather to state that the variable is not registered in the context of the script engine at all. Using a statement like if (abc = = undefined ), you get a second error hint similar to the one in the previous illustration.

In practice, if you use TypeOf to determine if a variable is undefined, it can be completely compatible with undefined (undefined) and uninitialized (uninitialized) two situations, but I don't like to use if (typeof xxx = = ' undefined ' ) this way of writing, because the literal string is easy to spell wrong, to use the strong type of language is used to the point of view is not professional.



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.