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 understand the equivalence comparison in javascript

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

Share

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

This article introduces the relevant knowledge of "how to understand the equivalence in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Equivalent comparison refers to comparing whether the values of two expressions (or operands) are equal, the comparison methods are: 1, the use of the "= =" operator, the syntax "Object.is (a, b)"; 2, the use of the "= =" operator, the syntax "Object.is (a, b)".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Equivalent comparison in javascript

In javascript, equivalence comparison refers to comparing whether the values of two expressions (or operands) are equal.

We all know that JavaScript is a loose type, and in some cases, when using = = for equivalence comparisons, it can give you unexpected results. This is because JavaScript implicitly casts the values of the two comparisons when using = = for an equivalent comparison.

0 = ='/ / truenull = = undefined / / true [1] = = true / / true

So JavaScript also provides us with three equal sign operators, which are stricter and do not cast comparisons, but practical = = comparisons are sometimes not the best solution:

NaN = NaN / / false

The good news is that there is a new, better and more accurate method in ES6, Object.is (), which has the same functionality as = = and performs well in some special cases:

Object.is (0,'); / / falseObject.is (null, undefined); / / falseObject.is ([1], true); / / falseObject.is (NaN, NaN); / / true

The following figure illustrates in detail the similarities and differences of the equivalent price comparison between =, and Object.is ():

This is the end of the content of "how to understand the equivalence comparison in javascript". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

*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