console.log( [] == ![] ) // true console.log( {} == !{} ) // false When comparing strings, numbers, and Boolean values for equality, the problem is relatively simple. But when it comes to comparing objects, the problem becomes complicated. The original equality and inequality operators in ECMAScript converted objects to similar types before performing the comparison. Later, some people questioned whether this conversion was reasonable. Finally, ECMAScript's solution is to provide two sets of operators:
The equality operator in ECMAScript is represented by two equal signs (==). If the two operands are equal, it returns true. This operator will first convert the operands (usually called forced conversion) and then compare their equality. When converting different data types, for equality and inequality operators: the following basic conversion rules are given in the book JS Elevation ① If one operand is a Boolean value, it is converted to a numeric value before comparing for equality—false is converted to 0 and true is converted to 1; ② If one operand is a string and the other operand is a number, convert the string to a number before comparing for equality ③. If one operand is an object and the other is not, call the object's valueOf() method and use the obtained primitive type value to compare according to the previous rules. These two operators follow the following rules when performing comparisons. ①, null and undefined are equal ② Before comparing for equality, null and undefined cannot be converted to any other value ③. If one operand is NaN, the equality operator returns false, while the inequality operator returns true. Important: Even if both operands are NaN, the equality operator returns false because, by rule, NaN is not equal to NaN. ④. If both operands are objects, compare whether they are the same object. If both operands point to the same object, the equality operator returns true; otherwise, it returns false Here is a digression: [] and {} are both reference types, which are stored in the heap memory, and in the stack memory there will be one or more addresses pointing to the data corresponding to this heap memory. So when using the == operator, for reference type data, what is compared is the address, not the actual value. Now let's discuss why the result of [] == ! [] is true①. According to the operator priority, ! The priority of is greater than ==, so it will be executed first![] ! Variables can be converted to boolean type. The negation of null, undefined, NaN, and empty string ('') is true, and the rest are false. So! The result after [] operation is false That is, [] == ! [] is equivalent to [] == false ② According to the rules mentioned above (if one operand is a Boolean value, it is converted to a numeric value before comparing equality - false is converted to 0 and true is converted to 1), you need to convert false to 0 That is, [] == ! [] is equivalent to [] == false, which is equivalent to [] == 0. ③. According to the rules mentioned above (if one operand is an object and the other operand is not, the object's valueOf() method is called, and the obtained primitive type value is compared according to the previous rules. If the object does not have a valueOf() method, toString() is called) For an empty array, [].toString() -> '' (returns an empty string) That is, [] == 0 is equivalent to '' == 0 ④ According to the rules mentioned above (if one operand is a string and the other operand is a number, the string is converted to a number before comparing equality) Number('') -> returns 0 This is equivalent to 0 == 0, which naturally returns true. To summarize:
The same is true for {} == !{}The key is {}.toString() -> NaN (returns NaN) According to the above rules (if one operand is NaN, the equality operator returns false) To summarize:
Then I believe that everyone can easily answer the following two questions. console.log( [] == !{} ) // true console.log( {} == ![] ) // false SummarizeThis concludes this article about the little-known JS issue that [] == ![] evaluates to true, while {} == !{} evaluates to false. For more information about JS [] == ![] evaluating to true, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: mysql5.7.22 download process diagram
>>: How to delete folders, files, and decompress commands on Linux servers
Recently, when doing homework, I needed to nest a ...
Copy code The code is as follows: <!DOCTYPE ht...
This article shares the specific code of Javascri...
The MySQL development team officially released th...
This article example shares the specific code of ...
Table of contents JS obtains the .txt file conten...
Over the past few years, there has been a trend i...
What is element-ui element-ui is a desktop compon...
1. Prerequisites We use the require.context metho...
Table of contents 1. Demand 1. Demand 2. SDK para...
Preface Although some love in this world has a pr...
Table of contents 1. v-for: traverse array conten...
This article describes how to use docker to deplo...
I saw that Taobao’s webpage uses import, while man...
Table of contents 1. The origin of tomcat 1. Tomc...