null, undefined and boolean

null and undefined

Overview

Both null and undefined can mean "no", and the meaning is very similar. Assigning a variable to undefined or null, to be honest, the grammatical effect is almost the same.

var a = undefined;
// or
var a = null;

In the above code, the variable a is assigned the values ​​of undefined and null respectively, and the effects of these two writing methods are almost equivalent.

In the if statement, they are automatically converted to false, and the equality operator (==) even directly reports that the two are equal.

if (!undefined) {
  console.log("undefined is false");
}
// undefined is false

if (!null) {
  console.log("null is false");
}
// null is false

undefined == null;
// true

From the code above, we can see how similar the behavior of the two is! The Dart language, an alternative to the JavaScript language developed by Google, clearly stipulates that there is only null and no undefined!

Since the meaning and usage are the same, why set two such values ​​at the same time? Isn't this unreasonably increasing the complexity and confuse beginners? This is related to historical reasons.

When JavaScript was born in 1995, initially like Java, only null was set to mean "none". According to the tradition of C language, null can be automatically converted to 0.

Number(null); // 0
5 + null; // 5

In the above code, when null is converted to a number, it automatically becomes 0.

However, Brendan Eich, the designer of JavaScript, felt that this was not enough. First of all, in the first version of JavaScript, null is treated as an object just like in Java. Brendan Eich thinks that the value of "none" is best not an object. Secondly, JavaScript at that time did not include an error handling mechanism. Brendan Eich felt that if null was automatically converted to 0, it would be difficult to find errors.

Therefore, he designed another undefined. The difference is as follows: null is an object that represents "empty", when converted to a value, it is 0; undefined is an original value that represents "no definition here", and when converted to a value, it is NaN .

Number(undefined); // NaN
5 + undefined; // NaN

Usage and meaning

For null and undefined, it can be roughly understood as follows.

null means a null value, that is, the value there is now empty. When a function is called, a parameter is not set with any value, then null can be passed in, which means that the parameter is empty. For example, a function accepts an error thrown by the engine as a parameter. If there is no error during operation, this parameter will be passed in null, indicating that no error has occurred.

undefined means "undefined", the following is a typical scenario of returning undefined.

// The variable is declared, but no value is assigned
var i;
i; // undefined

// When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined
function f(x) {
  return x;
}
f(); // undefined

// The object has no assigned attributes
var o = new Object();
op; // undefined

// When the function does not return a value, it will return undefined by default
function f() {}
f(); // undefined

Boolean value

Boolean values ​​represent the two states of "true" and "false". "True" is represented by the keyword true, and "false" is represented by the keyword false. Boolean values ​​have only these two values.

The following operators return Boolean values:

-The preceding logical operator: ! (Not) -Equality operator: ===, !==, ==, != -Comparison operators: >, >=, <, <=

If JavaScript expects a position to be a Boolean value, it will automatically convert the existing value at that position to a Boolean value. The conversion rule is that except for the following six values ​​are converted to false, other values ​​are regarded as true.

-undefined -null -false -0 -NaN -"" or '' (empty string)

Boolean values ​​are often used for program flow control, please see an example.

if ("") {
  console.log("true");
}
// no output

In the above code, the judgment condition after the if command is expected to be a boolean value, so JavaScript automatically converts the empty string to the boolean value false, causing the program to not enter the code block, so there is no output.

Note that the Boolean values ​​corresponding to empty arrays ([]) and empty objects ({}) are both true.

if ([]) {
  console.log("true");
}
// true

if ({}) {
  console.log("true");
}
// true

For more information about data type conversion, see the chapter "Data Type Conversion".