The triple equality operator === strictly checks if 2 values are the same:
1= = =1; // => true
1= = ='1'; // => false
1= = =true; // => false
Copy the code
However, the ES2015 specification introduces object.is (), which behaves almost the same as the strict equality operator:
Object.is(1.1); // => true
Object.is(1.'1'); // => false
Object.is(1.true); // => false
Copy the code
The main question is: When to use object.is () instead of strict equality checking? Let’s find out.
1. Strict equality check operator
First, let’s quickly review how the strict equality operator works.
The strict equality check operator evaluates to true when two values are of the same type and hold the same value.
For example, the following primitive values are equal because they are of the same type and have the same value:
1= = =1; // => true
'abc'= = ='abc'; // => true
true= = =true; // => true
null= = =null; // => true
undefined= = =undefined; // => true
Copy the code
Strict equality operators do not perform type casts, and operators of different types are not strictly equal even if they hold reasonably identical values:
1= = ='1'; // => false
1= = =true; // => false
null= = =undefined; // => false
Copy the code
When a strict equality check is performed on an object, the object is only strictly equal to itself:
const myObject = { prop: 'Value' };
myObject === myObject; // => true
Copy the code
Even if the attributes and values of two objects are exactly the same, they have different values:
const myObject1 = { prop: 'Value' };
const myObject2 = { prop: 'Value' };
myObject1 === myObject2; // => false
Copy the code
The above comparison schemes work in the same way in Object.is(valueA,valueB).
The difference between strict equality checking and object.is () is how NaN is handled and how negative zero-0 is handled.
First, NaN (non-numeric) is not strictly equal to any other value, even if another NaN is used:
NaN= = =NaN; // => false
NaN= = =1; // => false
Copy the code
Second, the strict equality operator cannot distinguish -0 from +0:
0= = = +0; // => true
Copy the code
Strict equality operators use strict equality comparison algorithms. www.ecma-international.org/ecma-262/7….
2. Object.is()
Object.is(valueA,valueB) checks arguments for equality in the same way as the strict equality operator, but with two differences.
First, NaN is equal to another NaN value:
Object.is(NaN.NaN); // => true
Object.is(NaN.1); // => false
Copy the code
Second, object.is () distinguishes -0 from +0:
Object.is(0, +0); // => false
Copy the code
Compared to the strict equality operator, Object.is() uses the same-value comparison algorithm. www.ecma-international.org/ecma-262/7….
conclusion
In most cases, the strict equality operator is a good way to compare values.
Object.is() is a good choice if you want to check NaN values directly or make a stricter distinction between negative and positive zeros.
Object.is() is also useful as a functional method for comparing values, for example in functional programming.
Source: DmitriPavlutin.com by Dmitri Pavlutin translated by The public account Front-end Full Stack Developer
This article is published on the public account “Front-end full stack Developer” ID: BY-Zhangbing-dev, the first time to read the latest article, will be published two days before the new article. After concern private letter reply: gift package, send some network high-quality video course network disk information, can save a lot of money for you!