The difference between undefined and null in JavaScript supplemental _javascript tips

Source: Internet
Author: User

Before the cloud-dwelling community sent a similar article
JavaScript null and undefined difference analysis

JavaScript undefined,null type and nan value difference

Let's talk about undefined:
The variables in JavaScript are weakly typed (I don't need to explain about this), so when declaring variables you just use the var keyword. If it is a strongly typed language like C, declaring the variable without specifying an initial value will give him a default value, such as the default value of the int variable is 0. But in a weak type language such as JavaScript, there is no way to determine what kind of default value to give such a variable, such as I declare a variable
VAR v1;
Is it a false or a 0, or a '?
cannot be determined because there is no type. In JavaScript, for a variable that does not have a given initial value after this life, give him a undefined. But the premise is that this variable must already be declared, and if there are no declared identifiers, there will be an error. Look at the code below.

Copy Code code as follows:

VAR v1;
Alert (v1); Undefined
Alert (v2); Error

To say that Null,javscript has several basic types, number,string,boolean,object. For a variable of type object, he has two situations, one is that he is an instance of an object, the other is an empty reference null, and a friend familiar with such object-oriented languages as Java should be easy to understand. For both cases, their type is object. A variable in JavaScript that is assigned to a value
Will determine its type, such as the following.
Copy Code code as follows:

var v1 = 1;
var v2 = true;

Alert (typeof v1); Number
Alert (typeof v2); Boolean

v2 = new Date ();
Alert (typeof v2); Object

v2 = "STR";
Alert (typeof v2); String

v2 = null;
Alert (typeof v2); Object

As you can see, NULL represents a special object type value in JavaScript, and is used to represent the concept of a null reference, and if you want to declare an identifier to be an object type, but do not give him an instance for the time being, you can initialize it to null first for later use.
Not necessarily absolutely correct, in short, for all variables, as long as the declaration has not specified the initial value, then he is undefined, if the object type is used to represent the concept of null reference, then is represented by NULL.
Here are some additions:
Null: indicates no value;
Undefined: Represents an undeclared variable, or a variable that is declared but not assigned, 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
Copy Code code as follows:

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):
What does the definition of an undefined undefined identifier mean in JScript to represent undefined? What is the difference and connection between it and "undefined" (including "included")? Why sometimes can use undefined to compare with variable, and sometimes not?
The difference between underfined and "undefined" can be seen at a glance. In general, we consider undefined to be a "keyword" provided by JScript, while "undefined" is a string with no suspense, but the contents of the quotation marks are the same as the undefined. Although the difference between undefined and "undefined" is very obvious, they are closely related.
Carefully read the JScript manual, in fact this underfined is a "defined" global value, rather than its literal meaning expressed in undefined. Let's look at the following code example, which is interesting:
Copy Code code as follows:

<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:
Copy Code code as follows:

<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 actually 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 both undefined (undefined) and uninitialized (uninitialized), but I don't like to use if (typeof xxx = = Undefined '), because the literal string is easy to spell wrong and is unprofessional in terms of the used strongly typed language.
Undefined: Not defined as declaring a variable without assigning it to it, or using an object property that does not exist
Null: null value This is really hard to understand.
As if to be compatible with previous browsers (IE4.0 only have undefined bar) and null and undefined are considered to be the same
Copy Code code as follows:

<script language= "JavaScript" >
var msg= "uses undeclared variables undef_x:";
if (typeof (undef_x) = = "undefined") msg+= "undefined";
else msg+= "defined";
msg+= "\ n";
msg+= is not assigned after declaring the variable undef_null_x: ";
var undef_null_x;
if (typeof (undef_null_x) = = "undefined") msg+= "undefined"; else msg+= "defined";
if (undef_null_x==null) msg+= "null"; else msg+= "not NULL";
msg+= "\ n";
msg+= "variable assignment null null_x:";
var null_x=null;
if (typeof (null_x) = = "undefined") msg+= "undefined"; else msg+= "defined";
if (null_x==null) msg+= "null"; else msg+= "not NULL";
msg+= "\ n";
msg+= "\" value x: ";
var x= "";
if (typeof (x) = = "undefined") msg+= "undefined"; else msg+= "defined";
if (x==null) msg+= "null"; else msg+= "not NULL";
Alert (msg);
</script>

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.