


The difference between equality judgment ===, == and Object.is() in js
I believe that people who are new to JS will be confused by its equality judgment. Take a look at the following code. How many can you answer correctly?
NaN === NaN // false NaN == NaN // false Object.is(NaN, NaN) // true 0 == false // true 1 == true // true Number(null) === 0 // true null == 0 // false
Javascript provides three different value comparison operations, namely strict equality, loose equality, and Object.is. After checking the information today, I made a summary. I hope the following content can be helpful to everyone. Please help. If there are any mistakes, you are welcome to correct me.
[Related course recommendations: JavaScript video tutorial]
1. Strict equality x === y judgment logic
1. If the data type of x is different from the data type of y, return false;
2. If x is Number type
● x is NaN, return false
● y is NaN, return false
● The value of x is equal to the value of y, return true
● x is 0, y is -0, return true
● x is -0, y is 0, return true
● Otherwise return false
3. Other types refer to SameValueNonNumber(x, y)
● Assertion: x, y is not Number type;
● Assertion: x, y have the same data type;
● x is undefined, y is undefined return true;
● x is null, y is null, return true;
● x is a string type, if and only if the character sequences of x and y are exactly the same ( The length is the same and the characters at each position are the same) return true, otherwise return false;
● If x is a Boolean type, return true when x and y are both true or false, otherwise return false ;
● If x is a symbol type, when x and y are the same symbol value, return true, otherwise return false;
● If x, y are the same object value, return true , otherwise return false;
NaN === NaN // false undefined === undefined // true null === null // true undefined === null // false
2. Relaxed equality x == y
1. If x and y have the same type, return the result of x===y ;
2. If x is null, y is undefined, return true;
3. If x is undefined, y is null, return true;
4. If If x is a numerical value and y is a string, return x == ToNumber(y);
5. If x is a string and y is a numerical value, return ToNumber(x) == y;
6. If x is a Boolean type, return the result of ToNumber(x)==y;
7. If y is a Boolean type, return the result of x==ToNumber(y);
8. If x is one of String, Number or Symbol and Type(y) is Object, return the result of x==ToPrimitive(y)
9. If Type(x) is Object and Type (y) is one of String, Number or Symbol, and returns the result of ToPrimitive(x)==y
10. Others return false
12 == '0xc' // true, 0xc是16进制 12 == '12' // true 12 == '12c' // false, 说明ToNumber转换是用的Number()方法
Note:
Number(null) === 0
But
null == 0 // false,
2.1 ToNumber converts a value to a numeric type
1. If it is a boolean type, true returns 1, false returns 0;
2. If it is a numerical value, it is simply passed in and returned;
3. If it is null, it returns 0
4. If it is undefined, it returns NaN;
5. If it is a string, if the string only contains numbers, convert it into a decimal number; if it is a valid floating point format, convert it into the corresponding floating point value; if it is binary or hexadecimal Convert it into the corresponding decimal value;
6. If it is an object, call the object's valueOf() method, and then convert it according to the previous rules. If the valueOf return value is NaN, call the toString() method, and then Convert the returned string according to the previous rules
2.2 ToPrimitive
toPrimitive(A) by trying to call A.toString() and A.valueOf() methods , convert parameter A into a primitive value (Primitive);
The primitive types in JS are: Number, String, Boolean, Null, Undefined;
The return of the valueOf() method of different types of objects Value:
Object | Return value |
---|---|
Array | Return the array object itself. |
Boolean | Boolean value |
Date | The stored time is from January 1, 1970 The number of milliseconds starting at midnight UTC |
Function | The function itself |
Number | Number value |
Object | The object itself. This is the default, you can override the valueOf method of the custom object |
String | String value |
// Array:返回数组对象本身 var array = ["ABC", true, 12, -5]; console.log(array.valueOf() === array); // true // Date:当前时间距1970年1月1日午夜的毫秒数 var date = new Date(2013, 7, 18, 23, 11, 59, 230); console.log(date.valueOf()); // 1376838719230 // Number:返回数字值 var num = 15.26540; console.log(num.valueOf()); // 15.2654 // 布尔:返回布尔值true或false var bool = true; console.log(bool.valueOf() === bool); // true // new一个Boolean对象 var newBool = new Boolean(true); // valueOf()返回的是true,两者的值相等 console.log(newBool.valueOf() == newBool); // true // 但是不全等,两者类型不相等,前者是boolean类型,后者是object类型 console.log(newBool.valueOf() === newBool); // false // Function:返回函数本身 function foo(){} console.log( foo.valueOf() === foo ); // true var foo2 = new Function("x", "y", "return x + y;"); console.log( foo2.valueOf() ); /* ƒ anonymous(x,y ) { return x + y; } */ // Object:返回对象本身 var obj = {name: "张三", age: 18}; console.log( obj.valueOf() === obj ); // true // String:返回字符串值 var str = "http://www.xyz.com"; console.log( str.valueOf() === str ); // true // new一个字符串对象 var str2 = new String("http://www.xyz.com"); // 两者的值相等,但不全等,因为类型不同,前者为string类型,后者为object类型 console.log( str2.valueOf() === str2 ); // false
3.同值相等
同值相等由 Object.is 方法判断:
● 两个值都是 undefined
● 两个值都是 null
● 两个值都是 true 或者都是 false
● 两个值是由相同个数的字符按照相同的顺序组成的字符串
● 两个值指向同一个对象
● 两个值都是数字并且
○ 都是正零 +0,
○ 或者都是负零 -0,
○ 或者都是 NaN
○ 都是除零和 NaN 外的其它同一个数字
Object.is('foo', 'foo'); // true Object.is(window, window); // true Object.is('foo', 'bar'); // false Object.is([], []); // false var foo = { a: 1 }; var bar = { a: 1 }; Object.is(foo, foo); // true Object.is(foo, bar); // false Object.is(null, null); // true Object.is(true, 'true') // false // 特例 Object.is(0, -0); // false Object.is(0, +0); // true Object.is(-0, -0); // true Object.is(NaN, 0/0); // true
4.零值相等
与同值相等类似,不过会认为 +0 与 -0 相等。
小结
=== 不做类型转换,当两边的数类型不相同时,直接返回false;当前类型相同且都是数值类型的时候,有一个是NaN,那么结果就是false, 另外 +0 === -0
==运算符,当两边操作数类不相同时会做隐式转换,然后才进行比较,这样的话就会出现 false == 0, '' == false 等现象, 但是Object.is不会做这种转换
本文来自 js教程 栏目,欢迎学习!
The above is the detailed content of The difference between equality judgment ===, == and Object.is() in js. 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











Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.
