JavaScript =, = =, = = = Difference detailed
Today found in project development in a small problem. When judging n== "" Result when n=0 n== "" The result also returns TRUE. Although it is a small problem, but has a big impact, so decided to go into the analysis.
1, = assignment operator
// Example: var n=1; Console.log (n); // 1n=n+1; Console.log (n); // 2
2. = = value comparison operator
The value comparison operator implicitly converts to the same data type when the data types are inconsistent on either side of the expression, and then compares the values.
// Example: var a= "1"; var b=1; Console.log (a==b); // true // Example: var a= ""; var b=0; Console.log (a==b); // true;
Note: because = = only compares values and does not perform data type comparisons, when the expressions on both sides are inconsistent, they are implicitly converted to the same data type and then compared,
So
0 is the logical false
1 is the logical true
An empty string is a logical false
var a= "";//Parse to False
var b=0;//parsing to False
So a==b False=false result true
3, = = = value type comparison operator
= = = The expression does not only compare values, but also to compare data types. Type conversions are not performed when the types are different, and in addition to comparing values, the data types on both sides are compared, which is the identity operator.
// a= "1" ; var b=1; Console.log (a ===b); // false // var a= " var b=0; Console.log (a ===b); // false; Console.log (false ===0); // false; Console.log ("1" = = = 1); // false;
4, = = and = = = Difference
4.1 There is a difference between = = and = = = When the same data type is compared.
4.2 Comparison between different types, = = will convert both sides of the expression to the same type after the comparison is equal, = = = If the type is different, do not convert, the result is unequal.
JavaScript =, = =, = = = Difference detailed