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
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 to undefined and "undefined (2007/1/30):
JScript is used to represent undefined identifier mean to be undefined? It and undefined to compare with variables, and sometimes not?
underfined and you can see it at a glance. In general cognition, we think undefined is a "keyword" provided by JScript, and There is no suspense is a string, only the contents of the quotation marks and undefined . undefined and "undefined" the difference is very obvious, But they are also closely linked.
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:
<script language= "JavaScript" >
alert (undefined);
alert (variable);
</script>
The result of the execution is:
Let's change the above code a little bit and add a typeof to see:
<script language= "JavaScript" >
Alert (typeof undefined);
Alert (typeof variable);
</script>
What should be the result? Show"Object"And"undefined"It? Of course not, two alert calls will be displayed"undefined"。
Soundefinedis a constant defined by the script engine that exists after the scripting engine is initialized. Its practical function is to useto represent the initialized state of a defined variable (uninitialized), such as Var i, then the value of this I isundefined, and I is a real define, just uninitialized. Then we can write this 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 notundefinedThe description is not initialized, but rather indicates that the variable was not enlisted in the context of the scripting engine at all. Use similar if (ABC = =undefined), you will 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 compatibleundefined (undefined)AndUninitialized (uninitialized)Two cases, just a lot when I don't like to use if (typeof xxx = =' undefined 'This way of writing, because the literal string is easy to spell wrong, the use of the strong type of language used to look at the point of view is not professional.
Null and undefined in JavaScript