Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Object.is () and strict equality operator in JavaScript

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

It is believed that many inexperienced people have no idea about how to use the Object.is () and strict equality operator in JavaScript. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The triple equality operator strictly checks whether the two values are the same:

1 = 1; / / > true 1 = '1mm; / / > false 1 = true; / / = > false

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

The main question is: when will Object.is () be used instead of strict equality checks? Let's find out.

1. Strict equality check operator

First, let's take a quick look at how the strict equality operator works.

When two values are of the same type and hold the same value, the strict equality check operator evaluates to true.

For example, the following original 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

Strict equality operators do not perform type casts, even if operators hold reasonable identical values, but different types of operators are not strictly equal:

1 = '1century; / / > false 1 = true; / / = > false null = undefined; / / = > false

When you perform a strict equality check on an object, the object is only strictly equal to itself:

Const myObject = {prop: 'Value'}; myObject = myObject; / / = > true

Even if the properties and values of the two objects are exactly the same, their values are different:

Const myObject1 = {prop: 'Value'}; const myObject2 = {prop:' Value'}; myObject1 = myObject2; / / = > false

The above comparison scheme works exactly the same in Object.is (valueA,valueB).

The difference between strict equality checking and Object.is () is how to handle NaN and how to handle negative zero-0.

First of all, NaN (non-numeric) is not strictly equal to any other value, even using another NaN:

NaN = NaN; / / = > false NaN = 1; / / = > false

Second, the strict equality operator cannot distinguish-0 from + 0:

-0 = = + 0; / / = > true

The strict equality operator uses the strict equality comparison algorithm.

Http://www.ecma-international.org/ecma-262/7.0/index.html#

Sec-strict-equality-comparison

2. Object.is ()

Object.is (valueA,valueB) checks for equality parameters in the same way as the strict equality operator, but with two differences.

First, NaN equals another NaN value:

Object.is (NaN, NaN); / / = > true Object.is (NaN, 1); / / = > false

Second, Object.is () distinguishes between-0 and + 0:

Object.is (- 0, + 0); / / = > false

Compared to the strict equality operator, Object.is () uses the same value comparison algorithm.

Http://www.ecma-international.org/ecma-262/7.0/index.html#sec-samevalue

In most cases, the strict equality operator is a good way to compare values.

If you want to check the NaN value directly or make a stricter distinction between negative and positive zeros, then Object.is () is a good choice.

Object.is () is also useful as a functional method for comparing values, such as in functional programming.

After reading the above, have you learned how to use the Object.is () and strict equality operator in JavaScript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report