【JavaScript】:理解 Undefined Null

Undefined
Null

1.Undefined

The Undefined type has only one value, which is the special value undefined. When a variable is declared with var or let but not initialized, it is equivalent to assigning an undefined value to the variable:
let message;
console.log(message == undefined);
//等同于
let message = undefined;
console.log(message == undefined);

Generally speaking, you never need to explicitly initialize with undefined. The purpose of adding this special value is to formally clarify the difference between a null object pointer and an uninitialized variable.

Variables that contain undefined values ​​are different from undefined variables.

let message;
console.log(message);//undefined
console.log(age);//报错

The first console.log will indicate the value of the variable message, which is "undefined".
The second console.log will output the value of an undeclared variable age, which will cause an error.

For undeclared variables, only one useful operation can be performed, which is to call typeof on it.

let message;
console.log(typeof message);//undefined
console.log(typeof age);//undefined

Whether declared or not, typeof returns the string "undefined". Logically speaking, this is correct, because although strictly speaking the two variables are fundamentally different, it is impossible to perform any real operation on any variable.

undefined is a false value. Therefore, if necessary, it can be detected in a more concise way. But keep in mind that there are many other possible values ​​that are also false. So be sure that what you want to detect is the literal value of undefined, not just a false value.

let message;//变量声明,只是值为undefined
if(message){
    
    
//这个块不会被执行
}
if(!message){
    
    
//这个块会执行
}
if(age){
    
    
//这里会报错
}

2.Null

The Null type also has only one value, the special value null. Logically speaking, the null value represents a null object pointer, which is why passing a null to typeof will return "object".
let car=null;
console.log(typeof car)//"Object"

When defining a variable that will save the object value in the future, it is recommended to initialize it with null instead of other values. In this way, as long as you check whether the value of this variable is null, you can know whether this variable was later assigned a reference to an object, such as:

if(car ! =null){
    
    
//car是一个对象的引用
}

The undefined value is derived from the null value, so ECMA-262 defines them as ostensibly equal, as shown in the following example:

console.log(null == undefined);//true

Using the equal operator (==) to compare null and undefined always returns true. But be aware that this operator will convert its operands for comparison.
Even if null and undefined are related, their uses are completely different. As before, you never have to explicitly set the variable value to undefined. But null is not the case. Whenever a variable wants to save an object, and there is no object to save at the time, it is necessary to fill the variable with null. In this way, it is possible to maintain the semantics of null being a pointer to a null object, and to further distinguish it from undefined.

Null is a false value. Therefore, if necessary, it can be detected in a more concise way. But keep in mind that there are many other possible values ​​that are also false. So be sure that what you want to detect is the literal value of null, not just a false value.

let message=null;//变量声明,只是值为undefined
let age;
if(message){
    
    
//这个块不会被执行
}
if(!message){
    
    
//这个块会执行
}
if(age){
    
    
//这个块不会执行
}
if(!age){
    
    
//这个块会执行
}

Guess you like

Origin blog.csdn.net/qq_43522998/article/details/112919570
Recommended