Reprint: https://www.cnblogs.com/weiqinl/p/9049745.html
There are two ways to compare JavaScript: the strict comparison operator and the conversion type comparison operator.
Corresponds ===
, and, in the equality operator !==
==
!=
.
Give me a chestnut first.
var str=' 1 'var NUM0=0var num1=1var BlT=Truevar BlF=Falsevar nul=NullVar und=UndefinedConsole.Log (str= = NUM1)TrueConsole.Log (str= = BlT)TrueConsole.Log (BlT= = NUM1)TrueConsole.Log (BlF= = NUM0)TrueConsole.Log (nul= = und)Trueconsole. Log (str === num1) //falseconsole. log (str === BlT) //false console. Log (BlT === num1) //falseconsole. log (BlF === NUM0) //false console. Log (nul === und) //false
The equality operator ( ==
) converts the type of operand of two different types and then makes a strict comparison.
The strict equality operator ( ===
) is also commonly called the congruent operator. The type is judged first, and then the values are compared for equality.
Type conversions
The way to type conversions:
type (x) |
type (y) |
Results |
Undefined |
Null |
True |
String |
Digital |
Tonumber (x) = = y |
Boolean value |
Digital |
Tonumber (x) = = y |
Object |
string/number |
Toprimary (x) = = y |
For two types, different operands are compared.
- String and Boolean values are converted to numeric types before being compared.
toNumber(x)
- For complex data types, it is converted to the original value before being compared against the previous rule.
x = toPrimary(obj) ==> toNumber(x)
And, how to turn to the original type value, usually automatically through the own valueOf()
method or toString()
method of implementation. If the conversion is not the original value, the comparison operation will report the type error.
具体过程:先执行`valueOf`方法,如果该方法返回一个原始值,则将这个原始值转换为数字,并返回这个数字,转换过程结束。如果返回非原始值,继续下一步。否则,执行`toString`方法,如果该方法返回一个原始值,则将这个原始值转换为数字,并返回这个数字,转换过程结束。如果返回非原始值 ,继续下一步。以上都没有成功转换为原始值,则抛出一个类型转换错误的异常。tips: 每个方法只执行一次。不会将方法返回的非原始类型值继续转换。
For an example, refer to the following:
The original data type (String,number,boolean,null, Undefined,symbol).
Complex data types (Object, Function, Array, Date, ...)
Comparison of the underlying data type, which is the comparison of the values, the comparison of the object objects, then the reference comparison
Equality operators ( ==
) to compare their values or object values after the type conversion.
For the equality operator ( ===
), the values and types on both sides of the equals sign must be exactly equal.
Why is it recommended to use
===
1. Increase understanding time when review code
In the ==
expressions used, we need to understand the author's intentions first.
- Take for granted that all can.
- It does need to be judged by type conversions.
- The type should not be converted, but it is wrong to write.
2. Errors will be judged
let num = 2let obj = { valueOf: function() { return 2 }}console.log(obj == 2)
Output true
,
We were meant to be both !=
, and the result here is that ==
this is not the result we want.
3. An exception is generated
let num = 2let obj = { valueOf: function() { return {} }, toString: function() { return {} }}console.log(obj === 2)console.log(obj == 2)
First line output:false
The second line outputs an error prompt:Uncaught TypeError: Cannot convert object to primitive value
With the previous type conversion, you can tell that, in general, the object is converted to the original value first.
The process is valueOf()/toString()
compared with other values by first converting to primitive value.
Here, we manually overwrite the Obj object valueOf()/toString()
, resulting in the inability to convert to the original value.
In general, it is recommended ===
to use unless you understand that a type conversion is really required.==
Conditional expression statements (if, trinocular operations, and so on)
if (condition) { statement1 } else { statement2}
The above is the underlying usage of the IF statement. Determine whether condition
to execute statement1
code block 1 or execute code block 2 by judging the Boolean value statement2
.
Let's look at the following example:
var s = ‘str‘var blT = truevar blF = falseif (s) { console.log(‘正确‘)} else { console.log(‘错误‘)}console.log(s == blT)
The above statement, the result is
正确false
Why the IF statement executes the first block of code. And it s == blT
's false?
In fact, this involves the problem of type conversion.
Output “正确”
:
- The conditional expression of the If statement automatically invokes the
Boolean()
conversion function, converting the result of the expression to a Boolean value.
- Boolean (' str ') = True
Output false
:
- The conversion
Number()
function is converted to a numeric value before being compared, as described in the section above for type conversion.
- Number (' str ') = NaN
- Number (true) = 1
Chart examples list Common object equality cases
Common equal
Exactly equal
Finish
in JavaScript = =,!==, = =, = = = Operator Summary