Until now, most people used either the  equals (==) or the identically equals (===) operator to compare two values. The latest was the recommended way to test for identity since it avoids performing type coercion during the comparison. Unfortunately, even the identity equals operator isn’t entirely accurate. Here’s an example:

+0 === –0 //returns true
NaN === NaN //returns false

Fortunately for us, the latest release of the spec introduces the Object.is method, which takes care of the quirks associated with the use of the equal operators. It accepts 2 arguments and returns true when those values are equivalent. Btw, equivalent here means that the values have the same type and the same value. Here’s how to get the correct results for the previous example:

Object.is( +0, –0); //false
Object.is( NaN, NaN); //true

Bottom line: Object.is produces the result that the identity equals operator should have produced for all the existing values and you should use it when writing JavaScript 6 code.

~ by Luis Abreu on December 1, 2014.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: