What is the purpose of Object.is?

11

I noticed that Javascript now has Object.is and according to the documentation:

  

Object.is determines whether two values match the same value.

About this I had some doubts:

  • Why Object.is , if I can compare the values with == or === ? If there is a difference between these forms, what are they?

  • Somehow does Object.is have any similarity to the instanceof operator? This doubt has come to me, since usually is is used in some languages to check if a given value corresponds to a particular object.

  • What does Object.is bring to Javascript?

asked by anonymous 23.10.2018 / 18:56

1 answer

12

Differences from operator === , according to the MDN documentation , are:

  • Object.is distinguishes +0 from -0 , but === does not distinguish.
  • Object.is considers two NaN as "equal" (equivalent to isNaN(a) && isNaN(b) ).
  • In the language specification (ES2015 / ES2016 onwards) there are four algorithms that equality. One is equal to == , other to === , third to Object.is , and fourth is used in some specific constructors and methods.

    This has nothing to do with instanceof , nor with inheritance. Equivalence algorithms do not look at this, when dealing with objects they only check whether the two are the same object or not. The benefit is to be able to distinguish +0 from -0 and perhaps the syntactic sugar to compare NaN .

        
    23.10.2018 / 19:04