Let's talk about undefined first:
Variables in Javascript are of weak type, so when declaring variables, you only need to use the var keyword. If it is a strongly typed language like C, if the initial value is not specified when declaring a variable, it will be given a default value, such as the default value of the int variable is 0. But in weak-type languages like Javascript, there is no way to determine what kind of default value should be given to such a variable, for example, I declare a variable
var v1;
Is it giving him false or 0, or ''?
Because there is no type, it is impossible to determine. In Javascript, for this variable that does not have an initial value after life, give it an undefined. However, the premise is that this variable must be declared, and an error will occur if it is not declared for an identifier. Check out the code below.
vo="vo";//Create a global variable without using the var keyword. If you do not assign a value, an error will be reported. As shown in the following
//v1;//There will be an error
var v2;//undeifned
var v3="";//null
alert(vo);
//alert(v1);//
alert(v2);
alert(v3);
Let's talk about null:
Javascript has several basic types, Number, String, Boolean, and Object. There are two situations for variables of Object type. One is that it is an instance of an object, and the other is an empty reference null. Friends who are familiar with object-oriented languages like Java should be easily understood. For both cases, their type is Object. Only when the variables in Javascript are assigned to them
It will determine its type, such as below.
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, which is used to represent the concept of null reference. If an identifier is declared as object type, but is not given an instance for the time being, then it can be initialized as null for later use.
It is not necessarily absolutely correct. Simply put, for all variables, as long as the initial value is not specified after declaration, it is undefined. If the Object type is used to represent the concept of null reference, it is expressed by null.
Here are some additions:
null : means no value;
undefined: Indicates an undeclared variable, or a variable that has been declared but has not been 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. Both are included with if (!object){}.