Boolean Indicates the Boolean data type

There are only two values true/false

Converts other types of data to Boolean types

[note] : only 0, NaN, “”, null, undefined five values are converted to false, the rest are converted to true (and no special cases)!!

There are two types of methods: manual conversion and implicit conversion

Manual switch

  1. Boolean([val])

    console.log(Boolean(0));//=>false
    console.log(Boolean(' '));//=>false
    console.log(Boolean(' '));//=>true
    console.log(Boolean(null));//=>false
    console.log(Boolean(undefined));//=>false
    console.log(Boolean(NaN));//=>false
    console.log(Boolean([]));//=>true
    console.log(Boolean([12]));//=>true
    console.log(Boolean(-1));//=>true
    Copy the code
  2. ! /!!!!!

    • ! : invert (first convert to Boolean, then invert)

    • !!!!! Boolean <=>Boolean <=>Boolean

      console.log(!1);//=>false
      console.log(!!1);//=>true
      Copy the code

Implicit conversion

conditional

  • If the condition is just a value, instead of ==/=== =/! = / > / < = =… By doing these comparisons, the value is first converted to a Boolean value and then verified

    if (1) {
        console.log('haha')}if('3px'+3) {//=> '3px+3' => TRUE
        console.log('haha')}if('3px'-3) {//=> NaN-3 => NaN =>FALSE
        console.log('haha')}Copy the code

Rules for converting data types during the == comparison

  • 【 Special points of the same type 】

    • {}=={}:false (object compares the address of heap memory)
    • []==[]:false (Array is an object, an instance of Array)
    • NaN==NaN:false (characteristic of NaN)
  • [Different types of conversion rules]

    1. Null == undefined results in trueBut convert to=== null===undefined (all ==(because the object type is inconsistent),The remaining null/undefined values are not equal to any other data type values
    	console.log(null= =undefined) //=>true
    	// Convert both to the number 0=>true
       console.log(null= = =undefined) //=>falseCopy the codeCopy the code
    1. The string == object converts the object to a string and then compares it
    2. In addition to these two cases,If the data types on both sides of == are inconsistent, they need to be converted to numbers for comparison
    / / = = exercises
    console.log([]==false);//true
    // An empty array object is compared with a Boolean value. If it is not converted to a number, it is converted to a string first.
    //[]->""->0// Empty string corresponds to 0
    //false->0 
    //
    console.log(! [] = =false)//false
    / / [] - > "" - > 0! 0->1 The type conversion itself has a high priority
    //false->0
    //
    // The object == is not a string == object of the same type, so it is converted to a number first (implicit conversion), and then compared
Copy the code

Null, and undefined

Null — represents an empty object pointer,

  • There is an object that represents “empty”, so the Number should be 0
  • Null means “no object”, meaning there should be no value.

Undefined means undefined,

  • Undefined means no assignment, no assignment means “no value”, and the value of “no value” converted to Number is NaN
  • Undefined means “missing value”, that is, there should be a value here, but it is not defined yet

Common uses of null and undefined

  • In current use, null and undefined are essentially synonymous, with only a few understanding differences.
  1. *** NULL means “no object”, that is, there should be no value. *** common:
  1. As arguments to a function, indicating that the function’s arguments are not objects.
	fn=val= >console.log(val);
    fn(null);
2.As the end of the object prototype chain.console.log(Object.prototype.__proto__);
    //null
Copy the code

  1. Undefined means “missing value”, that is, there should be a value here, but it is not defined yet.Common for:
    1. When a variable is declared but not assigned, it is undefined.
    2. The parameter is set when the function is defined, but no argument is passed when the function is called, so the value of the argument passed is equal to undefined.
    3. The object has no assigned attribute, the value of which is undefined.
    4. If the function returns no value, undefined is returned by default.
 	let a;
     console.log(a); // undefined
 		//
     let fn = function fn(val){
     	console.log(val)
         }
     fn() // undefined
 		//
     let  obj = new Object(a);console.log(obj.a); // undefined
 		//
     let b = fn();
     console.log(b); // undefined
Copy the code