Make a summary every day, persistence is victory!

/** @date 2021-06-23 @description Boolean Boolean method */Copy the code

One (Boolean)

Boolean is a basic data type of JS. It represents true or false and has only true and false values.

Declaration method:

  1. Literal declaration
const truly = true;
const falsy = false;
Copy the code
  1. Object statement
const trulyObj = new Boolean(true);
const falsyObj = new Boolean(false);
Copy the code

Non-boolean values can be passed directly to if statements:

The following values are falsy: 0, -0, false, NaN, null, undefined, ”, other values are turly;

Common methods:

  1. ToString: Returns a Boolean value as a string
const truly = true;
const falsy = false;

truly.toString(); // 'true'
falsy.toString(); // 'false'
Copy the code
  1. ValueOf: Returns the original value
const trulyObj = new Boolean(true); const falsyObj = new Boolean(false); const truly = trulyObj.valueOf(); // true const falsy = falsyObj.valueOf(); // false ### 2Copy the code

Two (null)

Null is also a basic data type of JS, which is used to represent objects that have not yet been set a value. Therefore, when declaring a value that can be determined as an object, the initial value can be assigned to null

Typeof null is’ object ‘

3 (undefined)

Undefined is a global variable whose value is undefined, and setting window.undefined = 1 is an invalid operation, indicating that a value is undefined

Three (Extended)

  1. use!!!!!Converts non-Boolean values to booleans quickly
!!!!! "'; // false !! 'null'; // true !! undefined; // false !! []; // trueCopy the code
  1. IsNaN (1 + null) is true, and isNaN(1 + undefined) is false