Home  >  Article  >  Web Front-end  >  The difference between undefined and null in javascript Supplementary article_javascript skills

The difference between undefined and null in javascript Supplementary article_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:32:171106browse

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 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


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) ){
alert("obj is null"); } if (nullobj == null){ alert("obj is null"); } if (nullobj == undefined){ alert("obj is undefined ");
}
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:
Copy code The code is as follows:


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