Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1109575 of Object.is()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/is
  • Revision title: Object.is()
  • Revision id: 1109575
  • Created:
  • Creator: DaSchTour
  • Is current revision? No
  • Comment Would like to preview, but it's not working, so adding this blind :(

Revision Content

{{JSRef}}

The Object.is() method determines whether two values are the same value.

Syntax

Object.is(value1, value2);

Parameters

value1
The first value to compare.
value2
The second value to compare.

Return value

A {{jsxref("Boolean")}} indicating whether or not the two arguments are the same value.

Description

Object.is() determines whether two values are the same value. Two values are the same if one of the following holds:

  • both {{jsxref("undefined")}}
  • both {{jsxref("null")}}
  • both true or both false
  • both strings of the same length with the same characters
  • both the same object
  • both numbers and
    • both +0
    • both -0
    • both {{jsxref("NaN")}}
    • or both non-zero and both not {{jsxref("NaN")}} and both have the same value

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

Examples

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

Polyfill for non-ES6 browsers

Object.is() is a proposed addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by using the following code at the beginning of your scripts. This will allow you to use Object.is() when there is no built–in support.

if (!Object.is) {
  Object.is = function(x, y) {
    // SameValue algorithm
    if (x === y) { // Steps 1-5, 7-10
      // Steps 6.b-6.e: +0 != -0
      return x !== 0 || 1 / x === 1 / y;
    } else {
      // Step 6.a: NaN == NaN
      return x !== x && y !== y;
    }
  };
}

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-object.is', 'Object.is')}} {{Spec2('ES6')}} Initial definition.
{{SpecName('ESDraft', '#sec-object.is', 'Object.is')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome("30")}} {{CompatGeckoDesktop("22")}} {{CompatNo}} {{CompatVersionUnknown}} {{CompatSafari("9")}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatUnknown}} {{CompatGeckoMobile("22")}} {{CompatNo}} {{CompatNo}} {{CompatSafariMobile("9")}}

See also

Revision Source

<div>{{JSRef}}</div>

<p>The <code><strong>Object.is()</strong></code> method determines whether two values are <a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness">the same value</a>.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<code>Object.is(<var>value1</var>, <var>value2</var>);</code></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>value1</code></dt>
 <dd>The first value to compare.</dd>
 <dt><code>value2</code></dt>
 <dd>The second value to compare.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>A {{jsxref("Boolean")}} indicating whether or not the two arguments are the same value.</p>

<h2 id="Description">Description</h2>

<p><code>Object.is()</code> determines whether two values are <a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness">the same value</a>. Two values are the same if one of the following holds:</p>

<ul>
 <li>both {{jsxref("undefined")}}</li>
 <li>both {{jsxref("null")}}</li>
 <li>both <code>true</code> or both <code>false</code></li>
 <li>both strings of the same length with the same characters</li>
 <li>both the same object</li>
 <li>both numbers and
  <ul>
   <li>both <code>+0</code></li>
   <li>both <code>-0</code></li>
   <li>both {{jsxref("NaN")}}</li>
   <li>or both non-zero and both not {{jsxref("NaN")}} and both have the same value</li>
  </ul>
 </li>
</ul>

<p>This is <em>not</em> the same as being equal according to the {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} operator. The {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} operator applies various coercions to both sides (if they are not the same Type) before testing for equality (resulting in such behavior as <code>"" == false</code> being <code>true</code>), but <code>Object.is</code> doesn't coerce either value.</p>

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

<h2 id="Examples">Examples</h2>

<pre class="brush: js">
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
</pre>

<h2 id="Polyfill_for_non-ES6_browsers">Polyfill for non-ES6 browsers</h2>

<p><code>Object.is()</code> is a proposed addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by using the following code at the beginning of your scripts. This will allow you to use <code>Object.is()</code> when there is no built–in support.</p>

<pre class="brush: js">
if (!Object.is) {
  Object.is = function(x, y) {
&nbsp;   // SameValue algorithm
    if (x === y) { // Steps 1-5, 7-10
&nbsp;     // Steps 6.b-6.e: +0 != -0
      return x !== 0 || 1 / x === 1 / y;
&nbsp;   } else {
&nbsp;     // Step 6.a: NaN == NaN
&nbsp;     return x !== x &amp;&amp; y !== y;
&nbsp;   }
  };
}
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.is', 'Object.is')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object.is', 'Object.is')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome("30")}}</td>
   <td>{{CompatGeckoDesktop("22")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatSafari("9")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("22")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatSafariMobile("9")}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness">Equality comparisons and sameness</a> — a comparison of all three built-in sameness facilities</li>
</ul>
Revert to this revision