Script House could not publish similar articles before
Analysis of the difference between JavaScript null and undefined
JavaScript Undefined, the difference between Null type and NaN value
Let’s talk about undefined first:
Variables in Javascript are weakly typed (I don’t think I need to explain this more), so you only need to use the var keyword when declaring a variable. If it is a strongly typed language like C, if no initial value is specified when declaring a variable, it will be given a default value. For example, the default value of an int variable is 0. But in a weakly typed language like Javascript, there is no way to determine what default value should be given to such a variable. For example, if I declare a variable
var v1;
, should I give it false or 0, or ' '?
Because there is no type, it cannot be determined. In Javascript, for such a variable that does not have an initial value given after its life, give it undefined. But the premise is that this variable must have been declared. If it is an undeclared identifier, an error will occur. Take a look at the code below. / /undefined
alert(v2); //Error report
Copy code
The code is as follows:
var v1 = 1;
var v2 = true ;
alert(typeof v2); //string
v2 = null;
alert(typeof v2); //object
You can see that null represents a special Object type value in Javascript. It is used to express the concept of null reference. If you want to declare an identifier as the object type, but do not give it to it for the time being. instance, then you can initialize it to null for later use.
It is not necessarily absolutely correct. Simply put, for all variables, as long as the initial value has not been specified after declaration, then it is undefined. If the Object type is used to express the concept of null reference, then it is represented by null.
The following are some additions:
null: means no value;
undefined: means an undeclared variable, or a variable that has been declared but has no value assigned, or a variable that does not exist Object properties. The == operator treats the two as equal. If you want to distinguish between the two, use the === or typeof operator.
Use if (!object){} to include both
Added: 2006.12.6
Copy code
The code is as follows: var obj = "aaa";
var nullobj;
if (obj == null || obj == undefined || (!obj) ){
}
if ( !nullobj ){
alert("! obj ");
}
Additional information about undefined and "undefined" (2007/1/30):
What does the undefined identifier used to indicate undefined in JScript mean? It is the same as "undefined" (including "). What are the differences and connections? Why can you sometimes use undefined to compare with variables, but sometimes not?
The difference between underfined and "undefined" can be seen at a glance. Under general understanding, we think that undefined is a "keyword" provided by JScript, but "undefined" is undoubtedly a string, except that the content within the quotation marks looks the same as undefined. Although the difference between undefined and "undefined" is very obvious, their connection is also close.
Read the JScript manual carefully. In fact, this underfined is a "defined" Global value, not undefined as expressed in its literal meaning. Let’s look at the following code example, it’s very interesting:
Copy the code
The code is as follows:
< script language="Javascript">
alert(undefined);
alert(variable);
The result of execution is:
Let’s slightly modify the above code and add a typeof call to see:
What should the result be now? Display "object" and "undefined"? Of course not, both alert calls will display "undefined".
So undefined is a constant defined by the script engine, which exists after the script engine is initialized. Its actual function is to indicate that a defined variable is in an initialized state (uninitialized), such as var i;. At this time, the value of i is undefined, and i is actually defined, but not initialized. At this time we can write such an expression to judge i, such as if (i == undefined). If a variable that has never appeared in the code is used, the concept of undefined at this time is not uninitialized as described by undefined, but indicates that the variable has not been registered in the context of the script engine at all. If you use a statement like if (abc == undefined), you will get an error message similar to the second one in the picture above.
In actual use, if you use typeof to determine whether a variable is undefined, it is fully compatible with both undefined and uninitialized situations, but I don’t like to use if in many cases (typeof xxx = = 'undefined' ) This way of writing is because literal strings are easy to misspell, and it also seems unprofessional from the perspective of using a strongly typed language that you are used to.
undefined: undefined, such as declaring a variable without assigning a value to it, or using a non-existent object property
null: null value This is really hard to understand
It seems to be for compatibility with previous browsers (Undefined is only available in IE4.0) and null and undefined are considered the same
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn