Comparative judgment
Comparison judgment is to compare two values and return a Boolean value to indicate whether the comparison conditions are met. JavaScript provides a total of 8 comparison operators. Here we mainly talk about the difference between strict equality operators and equality operators.
Strict equality operator===
Determine that the return of two values is different, false, both values are null/undefined/true/false true. One of the two values is NaN, false, both values are numeric and equal, true, both values are strings, and equal values are true, both values point to the same reference type true1 === "1" // falsetrue === true // trueundefined === undefined // true1 === 1 // trueNaN === NaN // false+0 === -0 // true({} === {}) // false[] === [] // false(function (){} === function (){}) // falseavar v1 = {};var v2 = v1; //Two values refer to the same object v1 === v2 // trueThe strict equality operator has a corresponding strict unequality operator (!==), and the operation results of the two are exactly the opposite.
Equality operator==
if equality operator compares data of the same type, the same strict equality operator else if equality operator compares different types of data: the original type of data will be converted into numeric types, converts both strings and booleans into numeric values, and then compares null == undefined Return true One is an object, the other is a number or string, converts the object into a basic type value and then compares else false123 == 123; //true'123' == 123; //true, '123' will be converted into numeric values 123false == 0; //true, false Converting to numeric values is 0'a' == 'A'; //false, the converted encoding is different 123 == {}; //false, executing toString() or valueOf() will change 123 == = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = NaN; //false, as long as there is NaN, it is false{} == {}; //false, the comparison is their address. The reference address of each newly created object is different null == undefined //true'NaN' == NaN //false123 == NaN //falseNaN == NaN //falsefalse == 0 //truetrue == 1 //truetrue == 2 //falseundefined == 0 //falsenull == 0 //false'123' == 123 //true'123' == 123 //falseThe equality operator has a corresponding unequal operator (!=), and the operation results of the two are exactly the opposite.
!!judge
!!Equivalent to Boolean , it is very useful when writing code!!Converting it to Boolean type for judgment is very useful
!!'xzavier'; // true!!''; // false!!'0'; // true!!'1'; // true!!'-1' // true!!0 // false!!undefined // false!!null // false!!NaN // false!!{}; // true!!{name:'xz'} // true!![]; // true!![1,2,3]; // true!!true; // true!judge
Inverse operator! is used to change the Boolean value to the opposite value, that is, true becomes false and false becomes true. For data that is not Boolean, the inversion operator automatically converts it to a Boolean. The rule is that the following six values are inverted to true, and the other values are inverted to false
undefined null false 0(including +0 and -0) NaN Empty string('')!undefined // true!null // true!false // true!0 // true!NaN // true!"" // true !54 // false!'hello' // false![] // false![1,2,3] // false!{} // false!{name:'xz'} // false[] and {} judgment
For [] and {}, we will definitely make judgments on them in the business code, as above
!!{}; // true!!{name:'xz'} // true!![]; // true!![1,2,3]; // true Can't be used for judgments! and!. For arrays, we use its length attribute to judge
[].length // 0 false[1,2,3].length // 3 true
For objects, you can use jQuery's method $.isEmptyObject(obj) , or you can encapsulate a method by imitating jQuery to write a
function isEmptyObject(obj) { var name; for ( name in obj ) { return false; } return true;}isEmptyObject({}); //trueisEmptyObject({name: 'xzavier'}); false Recommend a tool library underscore , which also has a method isEmpty(object)
const _ = require('underscore');_.isEmpty({}); // true&&judge
Used in conditional expressions, the rules are:
num1 && num2true true true true true false false false false false false false false
Used in a statement, the rules are:
result = expression1 && expression2
If expression1 evaluates to false, the result is expression1. Otherwise the result is expression2
(1 - 1) && ( x += 1) // 0(2 > 1) && ( 5 + 5) // 10(2 + 1) && ( 5 + 5) // 10
||Judgement
Used in conditional expressions, the rules are:
num1 || num2true true true true true false true true true false false false
Used in a statement, the rules are:
If the Boolean value of the first operator is true, the value of the first operator is returned and the second operator is no longer evaluated.
If the Boolean value of the first operator is false, the value of the second operator is returned
||Operators generally perform conditional expression judgment and fault tolerance processing in business code. If we cannot get the data when fetching it, it cannot affect the subsequent business code, so we need to be fault tolerance. || is a very good fault-tolerant writing method, which is equivalent to providing backup data.
var data = undefined || backup_data; //When there is an error in the request and the data is undefined, go to backup data backup_data
Three-eye judgment
rule:
condition ? expression1 : expression2;function absN(xzavier) { return xzavier > 0 ? xzavier : -xzavier;}absN(-123); //123absN(123); //123If the Boolean value of the first expression is true, the value of the second expression is returned, otherwise the value of the third expression is returned.
Summarize
The above is all the content of the judgment in JavaScript code for everyone. I hope the content of this article will be helpful to everyone using Javascript.