Similar articles have not been posted before.
Difference between JavaScript null and undefined
JavaScript Undefined, difference between the Null type and the NaN Value
Let's talk about undefined:
The variables in Javascript are of a weak type (I don't need to explain more about this), so you only need to use the var keyword to declare the variables. If a strong-type language like C is used to declare a variable without an initial value, a default value is given to it. For example, the default value of the int variable is 0. However, in a weak language such as Javascript, there is no way to determine the default value for such a variable. For example, I declare a variable.
Var v1;
Is it false, 0, or ''?
It cannot be determined because there is no type. In Javascript, an undefined variable is given to a variable without an initial value. However, the premise is that the variable must have been declared. If there is no declared identifier, an error will occur. Take a look at the following code.
Copy codeThe Code is as follows: var v1;
Alert (v1); // undefined
Alert (v2); // Error
Let's talk about null. Javscript has several basic types: Number, String, Boolean, and Object. There are two types of variables of the Object type: one is an instance of an Object, and the other is a null reference, users familiar with object-oriented languages like Java should be easy to understand. In both cases, both types are objects. The variable in Javascript is assigned a value to him.
Will determine its type, such as the following.Copy codeCode: 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
It can be seen that null represents a special Object type value in Javascript, which is used to represent the concept of null reference. If you want to declare an identifier as the object type, however, if the instance is not provided for the time being, it can be initialized to null for later use.
Not necessarily true. in simple words, as long as no initial value is specified for all variables after declaration, it is undefined. If the Object type is used to represent the concept of null reference, it is represented by null.
Below are some supplements:
Null: indicates no value;
Undefined: indicates an undeclared variable, a declared variable without a value assignment, or an object attribute that does not exist. The = Operator treats the two as equal. To distinguish the two, use the = or typeof operator.
Use if (! Object) {} both contain
Supplement: 2006.12.6Copy codeThe Code is 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 undefined and "undefined ):
In JScript, the undefined identifier is used to indicate what is undefined? What is the difference and connection between it and "undefined" (including? Why can't I use undefined to compare variables sometimes?
The difference between underfined and "undefined" can be seen at a glance. In general, we think that undefined is a "keyword" provided by JScript, but "undefined" is a string without any suspense, the content in the quotation marks is just like undefined. The difference between undefined and "undefined" is obvious, but they are closely related.
Read the JScript manual carefully. In fact, underfined is a "defined" Global value, rather than its literal meaning. The following code example is interesting:Copy codeThe Code is as follows: <script language = "Javascript">
Alert (undefined );
Alert (variable );
</Script>
The execution result is:
Let's slightly modify the above Code and add a typeof call to look at it:Copy codeThe Code is as follows: <script language = "Javascript">
Alert (typeof undefined );
Alert (typeof variable );
</Script>
What is the result? Show "object" and "undefined? Of course not. Both alert calls will display "undefined ".
Therefore, undefined is a constant defined by the script engine. It exists after the Script Engine Initialization is complete. The actual function is to indicate the initialization status (uninitialized) of a defined variable, such as var I;. In this case, the value of this I is undefined, I is actually define, but it is not initialized. In this case, we can write such an expression to judge I, for example, if (I = undefined ). If variables that have never been used in the Code are used, the undefined concept is not initialized as described by undefined, the variable is not registered in the context of the script engine. If (abc = undefined) statements are used, the second error message is displayed.
In actual use, if typeof is used to determine whether a variable is undefined, it is completely compatible with undefined and uninitialized, however, many times I do not like to use such expressions as if (typeof xxx = 'undefined'), because literal strings are prone to misspelling, it is not professional in terms of the strong language that you are used.
Undefined: undefined. For example, declare a variable without assigning a value to it, or use a non-existent object attribute.
Null: null. This is really hard to understand.
It seems that null and undefined are considered the same in order to be compatible with the previous browser (IE4.0 only has undefined ).Copy codeThe Code is as follows: <script language = "javascript">
Var msg = "use undeclared variable undef_x :";
If (typeof (undef_x) = "undefined") msg + = "undefined ";
Else msg + = "defined ";
Msg + = "\ n ";
Msg + = "undef_null_x:" is not assigned after the variable is declared :";
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 + = "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 + = "variable Fu \" \ "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>