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

A brief introduction of four Equality comparison algorithms in JavaScript

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "brief introduction of the four equal comparison algorithms in JavaScript". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the simple introduction of the four equal comparison algorithms in JavaScript".

There are four equality comparison algorithms in JavaScript operation:

Abstract equality comparison

Strict equality comparison

SameValueZero

SameValue

It can be said that as long as you have made an equality comparison, you are using one of the four algorithms. Because these algorithms are deployed in operators or methods that involve equality comparisons.

See the following table:

How to distinguish the differences between these algorithms can be divided into two aspects:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Whether type conversion will occur

Is there any special treatment for NaN,-0, + 0 (that is, 0) values

Here is a summary:

That is, abstract equality comparison algorithms cause type conversions, and the SameValue algorithm does not specifically treat values such as NaN,-0, and + 0.

How does the type conversion work?

Of the four algorithms discussed above, only the Abstract Equality comparison algorithm has type conversions. Type conversion occurs on the premise that the two values compared are of different types and that the conversion result is numeric.

For example:

1 = = 1 / / true (no type conversion occurs)'1' = = 1 / / true (type conversion occurs)

The first is easier to understand than true, and the second is different, where a type conversion occurs-- converting a non-numeric value to a number:'1' is different from 1, and'1' is a string, so it is converted to a number first-- that is, 1 (Number ('1')). The result is that two 1s are compared together, and the result is true.

Another quirk of the Abstract Equality comparison algorithm is that the specification defines null = = undefined to return the result as true. In theory, the type conversion should take place, and then compare, the result of the comparison should be false (the null is changed from 0 undefined to NaN, the two are different).

The specification makes a "small quirk" of the abstract equality comparison algorithm: the comparison between null and undefined is defined as true.

I suspect that this definition may be used to ensure forward integration (ti á n) ng, so we can keep it in mind. In other cases, the comparison results after type conversion shall prevail.

How are NaN,-0, and + 0 specially handled?

Before the publication of the ES2015 standard, there were no SameValueZero and SameValue algorithms. In other words, there are only two algorithms: abstract equality comparison and strict equality comparison: there is something wrong with these two algorithms-- they just can't distinguish between NaN and + 0 and-0. So before ES2015, we would encounter this comparison:

NaN = NaN / / false + 0 = =-0 / / true [NaN] .indexOf (NaN) / /-1 [0] .indexOf (- 0) / /-1

NaN is not equal to itself, + 0 equals-0. In our mind, we may not accept the result that "NaN is not equal to ourselves", but we can understand that "+ 0 equals-0" (all 0), but in some scenarios where symbol bits have meaning-means left, + means right, then it makes sense to distinguish between + 0 and-0.

Based on this, the SameValue algorithm is introduced into ES2015, which is used inside the Object.is () method to compare whether the two values are equal.

Object.is (NaN, NaN) / / true Object.is (0,-0) / / false

The SameValueZero algorithm, as we might guess from the name, differs from the SameValue algorithm only in its attitude towards "zero"-the SameValueZero algorithm cannot distinguish between + 0 and-0.

In other words:

Var array = [NaN,-0] array.includes (NaN) / / true array.includes (0) / / true

The result of array.includes (0) does not return false because the SameValueZero algorithm used internally cannot distinguish between + 0 and-0.

Thank you for your reading, the above is the content of "A brief introduction of the four equal comparison algorithms in JavaScript". After the study of this article, I believe you have a deeper understanding of the simple introduction of the four equal comparison algorithms in JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 241

*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