0

We know what the difference between == and === is - basically, === prevents Javascript engine to convert one of the parameter for making both parameters of the same type. But now, in ES6, came a new operator - Object.is which is a bit confusing (or maybe === is now confusing..)

From Mozila website (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) we can see the difference:

Sameness Comparisons:

x          y    ==      ===     Object.is
+0         -0   true    true    false
NaN        NaN  false   false   true

So, for me, looks like Object.is is even more strict in comparing parameters, if so, question raises - how unstrict was === (called "Strict Equality") :)

Julius Dzidzevičius
  • 10,775
  • 11
  • 36
  • 81

2 Answers2

1

Via MDN:

This is also not the same as being equal according to the === operator. The === operator (and the == operator as well) treats the number values -0 and +0 as equal and treats Number.NaN as not equal to NaN.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Ah, so this is a method.. Hm.. Thought its a comparison as well.. But, comparison is a method as well, right? Thx, my first day with Javascript.. – Julius Dzidzevičius Apr 18 '17 at 16:28
  • 1
    @J.D.: It *is* a comparison: Of the two arguments you give it, for equality (by one definition of equality; JavaScript has at least four). :-) – T.J. Crowder Apr 18 '17 at 16:29
  • @T.J. Crowder, so you are also saying that operators are not stored in memory (Execution Context), like functions, right? What made me confuse was this line from MDN - `The Object.is() method determines whether two values are the same value.` - they write **method**... – Julius Dzidzevičius Apr 18 '17 at 16:56
  • @J.D.: `Object.is` *is* a method. `===` is an operator. – T.J. Crowder Apr 18 '17 at 17:11
  • @T.J. Crowder - that was exactly what made me confuse - in ES6 they added a new operator which is also a method :) Quite interesting. Thx for sharing! – Julius Dzidzevičius Apr 18 '17 at 17:49
  • @J.D.: Fairly sure there aren't any new operators in ES2015 (aka "ES6")... :-) – T.J. Crowder Apr 18 '17 at 18:17
  • @T.J. Crowder, ok, its not **technically** called *operator*, but it does the same - takes to operands and returns a boolean - don't know any other element from Js engine that does the same + in MDN etc. its behaviour being compared with behaviour of other comparison operators, so... Send me hell, but I will call it Mr. Operator (from the methods) :) – Julius Dzidzevičius Apr 19 '17 at 04:36
  • @J.D. — Not all operators return a boolean. Not all operators compare things. – Quentin Apr 19 '17 at 06:20
  • @Quentin, yes I know, I didn't made it clear that I was talking about *comparison operators* - and they all return boolean, as far as I know – Julius Dzidzevičius Apr 19 '17 at 06:51
1

From the article you linked:

When to use Object.is versus triple equals

Aside from the way it treats NaN, generally, the only time Object.is's special behavior towards zeros is likely to be of interest is in the pursuit of certain meta-programming schemes, especially regarding property descriptors when it is desirable for your work to mirror some of the characteristics of Object.defineProperty. If your use case does not require this, it is suggested to avoid Object.is and use === instead. Even if your requirements involve having comparisons between two NaN values evaluate to true, generally it is easier to special-case the NaN checks (using the isNaN method available from previous versions of ECMAScript) than it is to work out how surrounding computations might affect the sign of any zeros you encounter in your comparison.

gforce301
  • 2,944
  • 1
  • 19
  • 24