Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the equivalence comparison in javascript

Shulou Source: shulou.com Published: 2022-06-01 16:42:27 09月27日 Update

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!

Tags: Case syntax operator operation practical two content that is Operand method more knowledge expression mandatory loose special same good unexpected learned Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Apple MariaDB MySQL