Examples of differences between null and undefined in JavaScript

Source: Internet
Author: User

Examples of differences between null and undefined in JavaScript

Let's talk about undefined:

Variables in Javascript are of a weak type. Therefore, when declaring a variable, you only need to use the var keyword. 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.

Vo = "vo"; // The global variable is created without the var keyword. If no value is assigned, an error is returned, as shown below:
// V1; // an error is returned.
Var v2; // undeifned
Var v3 = ""; // null
Alert (vo );
// Alert (v1 );//
Alert (v2 );
Alert (v3 );

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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 (! Both objects) {} are 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.