Use ‘>’ <‘ >=’ <=’ for non-numeric cases

When a non-numeric comparison is made, it is first converted to a numeric value and then compared.

// True converts to value 1 false converts to value 0
console.log(1 > true); // The result is false

console.log(1> =true); // Result is true

console.log(1 > '0'); // Result is true

console.log(1 > null); // Result is true

// Any value compared to NaN results in false
console.log(1 > 'hello'); // The result is false any value compared to NaN is false
Copy the code

If the symbol is flanked by a string, it is not converted to a numeric value for comparison, but the Unicode encoding of the characters in the string is compared separately. Note: So when comparing two string numbers, make sure you transform them first.

console.log('134545353' > '5'); // Return false without transformation

console.log('134545353' > +'5');// The result returns true
Copy the code

And in the comparison of character encoding, is a bit by bit comparison, if the first two sides of the symbol is the same, then compare the next bit, so this can be used to sort English, and compare Chinese is meaningless.

console.log('1' < '5'); // Result is true
console.log('11' < '5'); // The result is also true
console.log('be' > 'b'); // select * from b; // select * from b; // select * from b
console.log('be' < 'b'); // The result is false
Copy the code

&& with (true is true, false is false)

Can be applied to any value. If one of the operands is not of Boolean type, the logic and does not necessarily return Boolean type

1. If the first operand is null, NaN, undefined, false, 0, “” can be converted to false when the value of the return the value

console.log(null && 'world'); //null
Copy the code

2. When the first expression is true, the result of the entire expression depends on the second expression, which is returned

console.log('hello' && 'world'); //world
Copy the code

3. When the first expression is false, the result of the entire expression is determined and the first expression is returned

console.log(false && 'world'); //false
Copy the code

4. [null, NaN, undefined, false, 0, “”] directly return the operands

console.log(' ' && 123);  // "empty string
console.log(0 && null);  / / 0
console.log(123 && 345); / / 345
console.log(123 && undefined); //undefined
Copy the code

| | (there is only true, false, false)

1. If two or more operands are null, NaN, undefined, false, 0, “” can be converted to false when the value of the return the value.

console.log(null || false); //false
Copy the code

2. If the first operand is null, NaN, undefined, false, 0, “” returns the second operand.

console.log(null || 'hello'); //'hello'
Copy the code

3. If the first operand is true, return the first operand.

console.log(123||345);   / / 123
Copy the code

When the first expression is true, the result of the entire expression is determined, and the first expression is returned

When the first expression is false, the result of the entire expression depends on the second expression, which is returned

One of them is true and the result is true

The result is false only when both are false

! (NOT)

It is often used as the reverse operation of the condition judgment, type judgment, etc., and can also be used ‘! ‘Convert variable to Boolean type

console.log(!null);  //true

console.log(!undefined);  //true

console.log(!' '); //true

console.log(!100);  //false

console.log(!'abc');  //false
Copy the code