This is the 17th day of my participation in the August Text Challenge.More challenges in August

Boolean type

The Boolean type, the most commonly used type in JavaScript, has only two literals: true and false. These two values are not the same as numeric values, so true does not necessarily equal 1 and false does not necessarily equal 0. Here is an example of assigning a Boolean value to a variable:

var found = true;
var lost = false;
Copy the code

Note that Boolean literals true and false are case sensitive. That is, neither True nor False (or any other mixed case form) are Boolean values, just identifiers.

Although there are only two literals for Boolean types, all types of values in ECMAScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean, call the type conversion function Boolean(), as follows:

var msg = 'hello world';
var msgAsBool = Boolean(msg);
console.log(msgAsBool);//true
Copy the code

In this example, the string MSG is converted to a Boolean value, which is stored in the msgAsBool variable. The Boolean() function can be called on a value of any data type and always returns a Boolean, true or false depending on the data type being converted and its actual value. The following table shows the various data types and their corresponding conversion rules.

The data type The value converted to true Convert to a value of false
Boolean true false
String Any non-empty string An empty string
Number Any non-zero value (including infinity) 0 and NaN
Object Any object null
Undefined There is no undefined

These transformation rules are important to understand that flow control statements automatically perform the corresponding Boolean transformation. Look at the following code:

var msg = 'hello world';
if(msg){
    console.log('value is true');
}
Copy the code

Running this code displays a printout value is true because the string MSG is automatically converted to the corresponding Boolean value true. Because of this automatic Boolean conversion, it is important to know exactly what variable is used in the flow control statement. Using an object incorrectly instead of a Boolean value can drastically change the flow of an application.

Null type

The Null type is the second data type that has only one value, and this particular value is Null. From a logical point of view, null represents an empty object pointer, which is why object is returned when typeof is used to detect null values, as shown below:

var car = null;
console.log(typeof car);// object
Copy the code

If you define a variable to be used to hold objects in the future, it is best to initialize it to NULL rather than some other value. This way, you can tell if the corresponding variable already holds a reference to an object by simply checking the null value. Take the following example:

if(car ! =null) {// Perform some operations on the CAR object
}
Copy the code

In fact, undefined is derived from null, so ECMA-262 requires that their equality tests return true;

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

In the above code, undefined is derived from null, and two equals signs are used to check that they are equal, but they are not of the same type, so we return false when we use three equals signs.

Here the equality operator (==) between null and undefined always returns true. Although null and undefined have this relationship, their uses are completely different. As mentioned earlier, there is no need to display a variable as undefined in any case, and the same rules do not apply to null visible. In other words, whenever a variable intended to hold an object does not actually hold an object, it should explicitly be null. Doing so not only reflects the convention of null as a pointer to an empty object, but also helps further distinguish null from undefined.