Differences between undefined and null in javascript

Source: Internet
Author: User
In Javascript, two values are used to represent concepts similar to null values, undefined and null, which are easily obfuscated and represent two different concepts. 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.

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

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


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

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 ");
}


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:

The Code is as follows:


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.