mozilla

Revision 372713 of Object.is()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/is
  • Revision title: Object.is
  • Revision id: 372713
  • Created:
  • Creator: Waldo
  • Is current revision? No
  • Comment wordsmithing

Revision Content

{{harmony}}
{{fx_minversion_header("22")}}

Summary

Determines whether two values are the same value.

Method of Object
Implemented in JavaScript 1.8.5+
ECMAScript Edition ECMAScript 6th Edition

Syntax

var isSame = Object.is(value1, value2);

Parameters

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

Description

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

  • both undefined
  • both 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 NaN
    • or both non-zero and both not NaN and both have the same value

This is not the same as being equal according to the == operator.  The == operator applies various coercions to both sides 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 it treats NaN as not equal to NaN.

Compatibility

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 utilizing the following code at the beginning of your scripts. This will allow you to use Object.is when there is still no native support.

if (!Object.is) {
  Object.is = function(value1, value2) {
    if (v1 === 0 && v2 === 0)
      return 1 / v1 === 1 / v2;
    if (v1 !== v1 && v2 !== v2)
      return true;
    return v1 === v2;
  };
}

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

 

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatNo}}

{{CompatGeckoDesktop("22")}}

{{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoDesktop("22")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

 

Revision Source

<div>
  {{harmony}}</div>
<div>
  {{fx_minversion_header("22")}}</div>
<h2 id="Summary">Summary</h2>
<p>Determines whether two values are <dfn>the same value</dfn>.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of<code> <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object">Object</a></code></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code>var isSame = Object.is(value1, value2);</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <em><code>value1</code></em></dt>
  <dd>
    The first value to compare.</dd>
  <dt>
    <em><code>value2</code></em></dt>
  <dd>
    The second value to compare.</dd>
</dl>
<h2 id="Description">Description</h2>
<p><span class="short_text" id="result_box" lang="en"><span><code>Object.is()</code>&nbsp;determines whether two values are <strong>the same value</strong>. &nbsp;Two values are the same if one of the following holds:</span></span></p>
<ul>
  <li><span class="short_text" lang="en"><span>both <code>undefined</code></span></span></li>
  <li><span class="short_text" lang="en"><span>both <code>null</code></span></span></li>
  <li><span class="short_text" lang="en"><span>both <code>true</code> or both <code>false</code></span></span></li>
  <li><span class="short_text" lang="en"><span>both strings of the same length with the same characters</span></span></li>
  <li><span class="short_text" lang="en"><span>both the same object</span></span></li>
  <li><span class="short_text" lang="en"><span>both numbers and</span></span>
    <ul>
      <li>both <code>+0</code></li>
      <li>both <code>-0</code></li>
      <li>both <code>NaN</code></li>
      <li>or both non-zero and both not <code>NaN</code> and both have the same value</li>
    </ul>
  </li>
</ul>
<p>This is <em>not</em> the same as being equal according to the == operator. &nbsp;The == operator applies various coercions to both sides 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 === operator. &nbsp;The === operator (and the == operator as well) treats the number values -0 and +0 as equal, and it treats <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN" title="/en-US/docs/JavaScript/Reference/Global_Objects/Number/NaN"><code>NaN</code></a> as not equal to <code>NaN</code>.</p>
<h2 id="Compatibility" name="Compatibility">Compatibility</h2>
<p><code>Object.is</code>&nbsp;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 utilizing the following code at the beginning of your scripts. This will allow you to use <code>Object.is</code>&nbsp;when there is still no native support.</p>
<pre class="brush: js">
if (!Object.is) {
  Object.is = function(value1, value2) {
    if (v1 === 0 &amp;&amp; v2 === 0)
      return 1 / v1 === 1 / v2;
    if (v1 !== v1 &amp;&amp; v2 !== v2)
      return true;
    return v1 === v2;
  };
}
</pre>
<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>
<p>&nbsp;</p>
<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>{{CompatNo}}</td>
        <td>
          <p>{{CompatGeckoDesktop("22")}}</p>
        </td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>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>{{CompatGeckoDesktop("22")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision