What is the purpose of Object.is?

Asked

Viewed 193 times

12

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

Object.is determines whether two values correspond to the same value.

Some doubts have arisen about this:

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

  • Somehow, Object.is has some resemblance to the operator instanceof? This doubt came to me, because generally is is used in some languages to check if a given value corresponds to a given object.

  • What benefits Object.is brings Javascript?

  • 2

    To whom it concerns, there is an article in MDN dealing with: Comparisons of equality and uniformity

  • 1

    Wallace, I’m sorry. I had made the translation as a response and would adjust it to clear your doubts...

  • 1

    @Andréfilipe is all right. We only warned, because this practice here on the site results in removal of the response and, in more serious cases, even punishment by ban. It was good for us to warn.

  • 1

    It’s worth taking a look at the goal: @Andréfilipe: About copying articles

1 answer

14


Differences in relation to the operator ===, according to the documentation of the MDN, sane:

  1. Object.is distinguishes +0 of -0, but === does not distinguish.
  2. Object.is considers two NaN as "equal" (the equivalent of isNaN(a) && isNaN(b)).

In the language specification (ES2015/ES2016 onwards) there are four algorithms that deal with equality comparison. One equals the ==, other to the ===, a third to Object.is, and the fourth is used in some manufacturers and specific methods.

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

  • Thank you for your reply, young man.

  • 3

    Thanks for the "young" rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.