Skip to content

Instantly share code, notes, and snippets.

@ahouck
Last active February 19, 2016 00:36
Show Gist options
  • Save ahouck/d241b8d49ef4673b602d to your computer and use it in GitHub Desktop.
Save ahouck/d241b8d49ef4673b602d to your computer and use it in GitHub Desktop.
A intro to the new Object.is() feature in ES2015

Object.is() - A new comparator

This is not the same as being equal according to the == operator. The == operator applies various coercions to both sides (if they are not the same Type) before testing for equality (resulting in such behavior as "" == false being true), but Object.is doesn't coerce either value.

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.

Object.is('foo', 'foo'); // true Object.is(window, window); // true

Object.is('foo', 'foo');     // true
Object.is(window, window);   // true

Object.is('foo', 'bar');     // false
Object.is([], []);           // false

var test = { a: 1 };
Object.is(test, test);       // true

Object.is(null, null);       // true

// Special Cases
Object.is(0, -0);            // false
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment