preface

What is the essence? && and | | role is only one (definition) : Boolean and and or operation. Which variable is returned after the final result has been computed to a variable. && operator precedence than the | |.

In javascript: the following are treated as false: “(empty string), false, 0, null, undefined, NaN. These are also called virtual values in JS

1.&&The operator

Am&, which can be called a logical and, finds the first virtual-valued expression in its operands and returns it, or returns the last truth-valued expression if no virtual-valued expression is found. It uses a short circuit to prevent unnecessary work.

console.log(false && 1 && []);//false
// "this is not an empty string, there are Spaces in it
console.log(' '&& true && 5);/ / 5
Copy the code

Use if statements

const router: Router = Router();
router.get('/endpoint'.(req: Request, res: Response) = > {
   let conMobile: PoolConnection;
   try {
      //do some db operations
   } catch (e) {
   if(conMobile) { conMobile.release(); }}});Copy the code

Use the && operator

const router: Router = Router();

router.get('/endpoint'.(req: Request, res: Response) = > {
  let conMobile: PoolConnection;
  try{}catch (e) {
    conMobile && conMobile.release()
  }
});
Copy the code

2.||The operator

| | can call logic or, in its operands to find the first true value expression and returns it. This also uses a short circuit to prevent unnecessary work. It is used to initialize default parameter values in functions prior to supporting ES6 default function parameters.

console.log(null || 1 || undefined); / / 1

function logName(name) {
  var n = name || "Neck";
  console.log(n);
}

logName(); //Neck
Copy the code

3.!!!!!The operator

!!!!! The operator can cast the value on the right to a Boolean, which is an easy way to convert a value to a Boolean.

First introduction! ,! It’s usually the inverse

console.log(!null);//true
console.log(!false);//true
console.log(!0);//true
console.log(!' ');//true
console.log(!undefined);//true
console.log(!true);//false
Copy the code

Normal Objects cannot be empty

if(a! =null&&typeof(a)! =undefined&&a! =' ') {/ / a is not null
}
Copy the code

!!!!! usage

if(!!!!! a){/ / a is not null
}
Copy the code

Cast to a Boolean value using Boolean()

console.log(Boolean(null));//false
console.log(Boolean(undefined));//false
console.log(Boolean(' '));//false
console.log(Boolean(0));//false
console.log(Boolean(NaN));//false
console.log(Boolean(' '));//true
console.log(Boolean({}));//true
console.log(Boolean([]));//true
console.log(Boolean(1));//true
console.log(Boolean([].length));//false
Copy the code

Use!!!!! Method to cast to a Boolean value

console.log(!!null); // false
console.log(!!undefined); // false
console.log(!!' '); // false
console.log(!!0); // false
console.log(!!NaN); // false
console.log(!!' '); // true
console.log(!! {});// true
console.log(!! []);// true
console.log(!!1); // true
console.log(!! [].length);// false
Copy the code

From the above comparison, it is obvious!! The simplicity and convenience of the operator

4.= =and= = =The operator

== is used for general comparison, === is used for strict comparison, == can convert data types when comparing, === strict comparison, if the type does not match flase is returned.

  • = =The operator

Coercion is the process of converting a value to another type. In this case, == performs implicit coercion. Before comparing two values, == needs to perform some rules. Suppose we want to compare x equals y.

  1. ifxandyIs of the same type, JS will be replaced= = =Operator to compare.
  2. ifxfornull.yforundefined, the returntrue.
  3. ifxforundefinedandyfornull, the returntrue.
  4. ifxIs of typenumber.yIs of typestring, then returnx == toNumber(y).
  5. ifxIs of typestring.yIs of typenumber, then returntoNumber(x) == y.
  6. ifxFor the type isboolean, the returntoNumber(x)== y.
  7. ifyFor the type isboolean, the returnx == toNumber(y).
  8. ifxisstring,symbolornumberAnd theyIf the value is object, returnx == toPrimitive(y).
  9. ifxisobject.yisstring.symbolIt returnstoPrimitive(x) == y.
  10. The rest goes backfalse

example

x y x == y eligible
6 6 true Condition 1 is of the same type
6 ‘6’ true Condition 4
null undefined true Condition 2
0 false true Condition 7 y is a Boolean type
‘6, 7’ [1, 2] true Condition 8 converts an array to a string using the toString() method, which returns 1,2
‘[object Object]’ {} true Condition 8 Converts an object to a string using the toString() method, which returns [object object]
console.log(6= =6);//true
console.log(6= ='6');//true
console.log(null= =undefined);//true
console.log(0= =false);//true
console.log('5 or 6'= = [5.6]);//true
console.log('[object Object]'= = {});//true
Copy the code
  • = = =The operator
x y x === y instructions
6 6 true The value and type are the same
6 ‘6’ false Values and types are different
null undefined false Values and types are different
0 false false Values and types are different
‘6, 7’ [1, 2] false Values and types are different
‘[object Object]’ {} false Values and types are different

Using the === operator, all comparisons except the first example will return false because they are of different types, and the first example will return true because both have the same type and value.

console.log(6= = =6);//true
console.log(6= = ='6');//false
console.log(null= = =undefined);//false
console.log(0= = =false);//false
console.log('5 or 6'= = = [5.6]);//false
console.log('[object Object]'= = = {});//false
Copy the code
let a = { a: 1 };
let b = { a: 1 };
let c = a;
console.log(a === b); // Print false, even if they have the same property
console.log(a === c); // true
Copy the code

JS compares objects and primitive types in different ways. In primitive types, JS compares them by value, while in objects, JS compares variables by reference or by an address in memory where they are stored. This is why the first console.log statement returns false and the second console.log statement returns true. A and C have the same reference address, while A and B do not.

conclusion

Learning makes me happy! One unit!