diff --git a/files/en-us/web/javascript/reference/global_objects/object/is/index.html b/files/en-us/web/javascript/reference/global_objects/object/is/index.html index 3b363828cf10f31..2f73243b3634c5e 100644 --- a/files/en-us/web/javascript/reference/global_objects/object/is/index.html +++ b/files/en-us/web/javascript/reference/global_objects/object/is/index.html @@ -68,32 +68,39 @@

Description

This is also not the same as being equal according to the {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}} operator. The - === operator (and the == operator as well) treats the number - values -0 and +0 as equal and treats {{jsxref("Number.NaN")}} - as not equal to {{jsxref("NaN")}}.

+ only difference between Object.is() and === is in + their treatment of signed zeroes and NaNs. For example, the === + operator (and the == operator) treats the number values -0 + and +0 as equal. Also, the === operator treats + {{jsxref("Number.NaN")}} and {{jsxref("NaN")}} as not equal.

Examples

Using Object.is

-
Object.is('foo', 'foo');     // true
-Object.is(window, window);   // true
-
-Object.is('foo', 'bar');     // false
-Object.is([], []);           // false
-
+
// Case 1: Evaluation result is the same as using ===
+Object.is(25, 25);                // true
+Object.is('foo', 'foo');          // true
+Object.is('foo', 'bar');          // false
+Object.is(null, null);            // true
+Object.is(undefined, undefined);  // true
+Object.is(window, window);        // true
+Object.is([], []);                // false
 var foo = { a: 1 };
 var bar = { a: 1 };
-Object.is(foo, foo);         // true
-Object.is(foo, bar);         // false
-
-Object.is(null, null);       // true
-
-// Special Cases
-Object.is(0, -0);            // false
-Object.is(0n, -0n);          // true
-Object.is(-0, -0);           // true
-Object.is(NaN, 0/0);         // true
+Object.is(foo, foo);              // true
+Object.is(foo, bar);              // false
+
+// Case 2: Signed zero
+Object.is(0, -0);                 // false
+Object.is(+0, -0);                // false
+Object.is(-0, -0);                // true
+const n = 15;
+Object.is(0n, -0n);               // true
+
+// Case 3: NaN
+Object.is(NaN, 0/0);              // true
+Object.is(NaN, Number.NaN)        // true
 

Polyfill

@@ -102,11 +109,16 @@

Polyfill

  Object.defineProperty(Object, "is", {   value: function (x, y) { // SameValue algorithm - if (x === y) { // Steps 1-5, 7-10 - // Steps 6.b-6.e: +0 != -0 + if (x === y) { + // return true if x and y are not 0, OR + // if x and y are both 0 of the same sign. + // This checks for cases 1 and 2 above. return x !== 0 || 1 / x === 1 / y; } else { - // Step 6.a: NaN == NaN + // return true if both x AND y evaluate to NaN. + // The only possibility for a variable to not be strictly equal to itself + // is when that variable evaluates to NaN (example: Number.NaN, 0/0, NaN). + // This checks for case 3. return x !== x && y !== y; }   }