Home  >  Article  >  Web Front-end  >  Introduction to the difference between null and undefined in JavaScript_javascript skills

Introduction to the difference between null and undefined in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:22:311062browse

There are two special values ​​in JavaScript that represent the absence of information: null and undefined. Personally, I think the difference between these two special values ​​can be understood from the following perspective:

1.null represents a container that stores information (such as a variable that has been assigned a value before), but the content in the container is empty.
2. undefined means that there is no container for storing information.

The null in JavaScript is no different from the null in most other programming languages. It is basically used to indicate that the information value is empty; in JavaScript, the expression returns an undefined result in the following situations:

1. A variable that has never been assigned a value.
2. Access a property value that does not exist in an object.
3. Access members that do not exist in the array.
4. Call a function without a return statement.
5. Call the function whose return statement is empty ("return;").

In fact, like Infinity and NaN, undefined is a global variable in JavaScript and can even be assigned other values ​​in ECMAScript 3. ECMAScript 5 corrects this error and makes undefined variables read-only.

For comparison between null and undefined, you can use the === congruence operator. If you use the ordinary == operator, null and undefined are equivalent:


Copy code The code is as follows:

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


During programming, if you need to assign a null value to a variable, you generally use null instead of undefined. The reason is:

1. undefined is generally considered to be a lack of information at the system level and error reporting level.
2. Null is generally considered to be an empty information value at the programming level and logical operation level.

If type conversion is involved in the program, then when converting to number type, the results of null and undefined are different:

1. The result of converting undefined to number is NaN.
2. The result of converting null to number is 0.

It is worth mentioning that the result of converting empty strings and empty arrays into numbers is also 0.

As for why two values ​​representing “none” are designed in JavaScript, please refer to Ruan Yifeng’s blog post.

Experiment

In the following experimental code, the expression results are all undefined:

Copy code The code is as follows:

var a;
console.log(a);

function Sample(x){
this.x = x;
}
var s = new Sample();
console.log(s.x)
console.log(s.notExistVariable);

var n = [2,3,4];
console.log(n[8]);

function test(){
//no return value for this function
}
console.log(test());

function test2(){
Return;
}
console.log(test2());

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