Home >Web Front-end >JS Tutorial >Example introduction to the difference between null and undefined in JavaScript_javascript skills

Example introduction to the difference between null and undefined in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:36:261129browse

Let’s talk about undefined first:

Variables in Javascript are weakly typed, so you only need to use the var keyword when declaring variables. 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 it be false, 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.

vo="vo";//If you do not use the var keyword, you will create a global variable. If you do not assign a value, an error will be reported, as follows
//v1;//will report an error
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. For Object type variables, there are two situations: one is that it is an instance of an object, and the other is that it is a null reference. Friends who are familiar with object-oriented languages ​​​​such as Java should easily understand. In both cases, their type is Object. Variables in Javascript are only used when assigning values ​​
It 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. It is used to express the concept of null reference. If you want to declare an identifier as an object type, but do not give it an instance for the time being, then you can Initialize it to null for later use.
It may not be 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.

Here are some additions:

null: means no value;
undefined: Indicates an undeclared variable, a variable that has been declared but has no value assigned, or an object property that does not exist. 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.

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