


Detailed explanation of the contents of ==, === and Object.js() in js (comprehensive)
This article brings you a detailed explanation of ==, === and Object.js() in js (comprehensive). It has certain reference value. Friends in need can refer to it. I hope It will help you.
This article mainly explains the three equality operations in JavaScript: ==, === and Object.js(). Deepen everyone's impression through comparison and examples, and provide detailed explanations of individual examples.
Preparatory knowledge
=== operator
For x === y, the comparison steps of this operator are as follows:
1. If the type of x is different from the type of y, return false;
2. If the type of x is a number , then:
a. If x is NaN, return false;
b. If y is NaN, return false;
c. If x and y are the same Numeric value, returns true;
d. If x is 0 and y is -0, returns true;
e. If x is -0 and y is 0, returns true;
f. Return false;
3. Return the result of SameValueNonNumber(x, y).
SameValueNonNumber(x, y) abstract operation compares two non-numbers and whether x and y of the same type are equal. The comparison steps are as follows:
1. If the type of x is null or undefined, Return true;
2. If x is a string type,
If x and y are exactly the same character encoding sequence, return true, otherwise return false;
3 , If x is a Boolean type,
4. If x and y are both true or false, return true, otherwise return false;
5. If x is a symbol type,
If x and y are the same symbol value, return true, otherwise return false;
6. If x and y are the same object value, return true, otherwise return false.
The points to note are NaN, 0, -0:
NaN === NaN // false +0 === -0 // true -0 === +0 // true
These three examples correspond to 2.1, 2.4 and 2.5 in the x === y comparison step respectively. The output results of these three examples are completely in accordance with the definitions of the specifications. There is no reason, this is how the specifications are defined. As for why the specification is defined in this way, you may need to ask the makers of the specification. This is beyond the scope of this article.
Object.is()
For Object.is(x, y), the abstract operation SameValue(x, y) will be used for comparison. The steps of this abstract operation are as follows:
1. If the type of x is different from the type of y, return false;
2. If the type of x is a number, then:
a. If both x and y are NaN, returns true;
b. If x is 0 and y is -0, returns false;
c. If x is -0 and y is 0, returns false;
d. If x and y are the same numeric value, return true;
e, return false;
3. Return the result of SameValueNonNumber(x, y).
It can be seen that the difference between === and Object.is() lies in the processing of NaN and signed 0:
NaN === NaN // false +0 === -0 // true -0 === +0 // true Object.is(NaN, NaN) // true Object.is(+0, -0) // false Object.is(-0, +0) // false
==Operator
For x == y, the comparison steps of this operator are as follows:
1. If x and y are of the same type, return the result of x === y;
2. If x is null , y is undefined, return true;
3. If x is undefined and y is null, return true;
4. If the type of x is a number and the type of y is a string, Return the result of x == ToNumber(y);
5. If the type of x is a string and the type of y is a number, return the result of ToNumber(x) == y;
6. If the type of x is a Boolean type, return the result of ToNumber(x) == y;
7. If the type of y is a Boolean type, return the result of x == ToNumber(y);
8. If the type of x is one of string, number or Symbol, and the type of y is an object, return the result of x == ToPrimitive(y);
9. If x The type is an object, and the type of y is one of string, number, or Symbol. The result of ToPrimitive(x) == y is returned;
10. Return false.
The method ToNumber was used above, and the steps for ToNumber(x) are as follows:
1. If the type of x is Undefined, return NaN;
2. If the type of x If it is Null, return 0;
3. If the type of x is a Boolean type, return 1 if x is true, and return 0 if false;
4. If the type of x is a number, return x;
5. If the type of x is a string, the reference string is converted into a number. This article will not introduce this content in detail;
6. If the type of x is a Symbol, NaN is returned;
7. If the type of x is an object,
a. Let the value of primValue be ToPrimitive(x, hint Number);
b. Return the result of ToNumber(primValue) ;
[] == ![]
The comparison steps of the == operator are discussed above. Let’s talk about an example to deepen our impression:
[] == ![]
First, the left [] is an empty array, the type is object, the right side is ![], [] is a true value, so the result of ![] is false:
[] == ![] // => [] == false
Then it will go to the x == y comparison step In step 7, return the result of x == ToNumber(y), that is:
[] == false // => [] == ToNumber(false)
From step 3 of ToNumber(x), we know that ToNumber(false) returns 0:
[] == ToNumber(false) // => [] == +0
Then go to step 9 of the x == y comparison step and return the comparison result of ToPrimitive(x) == y:
[] == +0 // => ToPrimitive([]) == +0
The result of ToPrimitive([]) is the empty string "", please give the reason Check out the article ToPrimitive abstract operation in the ECMAScript7 specification. Therefore, the above is equivalent to:
"" == +0
Then go to step 5 of the x == y comparison step and return the result of ToNumber(x) == y:
"" == +0 // => ToNumber("") == +0
由ToNumber操作的第5步可知,ToNumber("")的结果是+0,所以也就是:
+0 == +0 // true
{} == !{}
首先,左边是{},类型是对象,右边是!{},{}是真值,所以!{}是false:
{} == !{} // => {} == false
然后同样会走到x == y比较步骤的第7步,返回x == ToNumber(y)的结果,也就是:
{} == false // => {} == ToNumber(false)
由ToNumber(x)的第3步可知,ToNumber(false)返回+0:
{} == ToNumber(false) // => {} == +0
然后走到x == y比较步骤的第9步,返回ToPrimitive(x) == y的比较结果:
{} == +0 // => ToPrimitive({}) == +0
ToPrimitive({})的结果是字符串"[object Object]",原因请查看文章ECMAScript7规范中的ToPrimitive抽象操作。所以,上面等价于:
"[object Object]" == +0
然后走到x == y比较步骤的第5步,返回ToNumber(x) == y的结果:
"[object Object]" == +0 // => ToNumber("[object Object]") == +0
由ToNumber操作的第5步可知,ToNumber("[object Object]")的结果是NaN,所以也就是:
NaN == +0 // false
所以,[] == ![]的结果是true,{} == !{}的结果是false。可能有人第一次看到[] == ![]的时候,觉得这个的比较结果怎么可能是true。我觉得有时候不要感性的去认识问题,按照规定的运算步骤走一遍,结果是什么就是什么。
总结
本文讲解了JavaScript中的三种相等运算:==,===和Object.js(),希望对大家有所帮助。
The above is the detailed content of Detailed explanation of the contents of ==, === and Object.js() in js (comprehensive). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
