Examples of null and undefined differences in JavaScript introduce _javascript tips

Source: Internet
Author: User

Let's talk about undefined:

The variables in JavaScript are weakly typed, so you need to use the VAR keyword when declaring variables. If it is a strongly typed language like C, declaring the variable without specifying an initial value will give him a default value, such as the default value of the int variable is 0. But in a weak type language such as JavaScript, there is no way to determine what kind of default value to give such a variable, such as I declare a variable

VAR v1;

Is it a false or a 0, or a '?

cannot be determined because there is no type. In JavaScript, for a variable that does not have a given initial value after this life, give him a undefined. But the premise is that this variable must already be declared, and if there are no declared identifiers, there will be an error. Look at the code below.

Vo= "VO"; the global variable is not created using the var keyword, and if not assigned, the error is as follows
v1;//will complain.
var v2;//undeifned
var v3= "";//null
Alert (VO);
Alert (v1);//
Alert (v2);
Alert (v3);

Again, say null:

Javscript has several basic types, number,string,boolean,object. For a variable of type object, he has two situations, one is that he is an instance of an object, the other is an empty reference null, and a friend familiar with such object-oriented languages as Java should be easy to understand. For both cases, their type is object. A variable in JavaScript that is assigned to a value
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

As you can see, NULL represents a special object type value in JavaScript, and is used to represent the concept of a null reference, and if you want to declare an identifier to be an object type, but do not give him an instance for the time being, you can initialize it to null first for later use.
Not necessarily absolutely correct, in short, for all variables, as long as the declaration has not specified the initial value, then he is undefined, if the object type is used to represent the concept of null reference, then is represented by NULL.

Here are some additions:

Null: indicates no value;
Undefined: Represents an undeclared variable, or a variable that is declared but not assigned, or an object property that does not exist. the = = operator regards both as equal. If you want to distinguish between the two, use the = = = or typeof operator. The use of if (!object) {} is both included.

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.